Move folder if certain file is available using powershell or batch script

Status
Not open for further replies.

Gon

Active Member
538
2014
149
14,160
I have around 100~150 folder. each folder should have file "post.txt". however sometimes some folder missing post.txt. i want to move those folder to other directory using powershell or batch script only if folders contains "post.txt" . it will be really helpful if someone can help me.
 
5 comments
okay do that
Post automatically merged:

import shutil,os # PATH OF FOLDER TO MOVE THE FOLDERS WITHOUT POST.TEXT folders_without_post_text_path = '/Desktop/folderName/' # PATH WHERE ALL THE FOLDERS ARE LOCATED folder_with_all_folders_path = '/Desktop/folderName/' # IT WILL GET ALL YOUR FOLDERS allFolders = os.listdir(folder_with_all_folders_path) for folder in allFolders: folderPath=folder_with_all_folders_path+folder folderToBeMovePath=folders_without_post_text_path+folder # OPEN EACH FOLDER TO SEE IF IT CONTAINS "POST.TEXT" folderToCheck = os.listdir(folderPath) if 'post.txt' not in folderToCheck: shutil.move(folderPath,folderToBeMovePath)
Post automatically merged:

import shutil,os # PATH OF FOLDER TO MOVE THE FOLDERS WITHOUT POST.TEXT folders_without_post_text_path = '/Desktop/folderName/' # PATH WHERE ALL THE FOLDERS ARE LOCATED folder_with_all_folders_path = '/Desktop/folderName/' # IT WILL GET ALL YOUR FOLDERS allFolders = os.listdir(folder_with_all_folders_path) for folder in allFolders: folderPath=folder_with_all_folders_path+folder folderToBeMovePath=folders_without_post_text_path+folder # OPEN EACH FOLDER TO SEE IF IT CONTAINS "POST.TEXT" folderToCheck = os.listdir(folderPath) if 'post.txt' not in folderToCheck: shutil.move(folderPath,folderToBeMovePath)

change the first /Desktop/folderName/ to the path where you want to move the folders without post.text > 2nd change the second /Desktop/folderName/ to the folder path where you have all the folders located > create a file name script.py and save the code inside > move it to your Desktop > open your terminal > cd Desktop > and finally run python3 ./script.py
 
Last edited:
  • Like
Reactions: Gon
Python:
import os, glob
WithPosts = "WithPosts"

WorkingFolder = os.getcwd()

if os.path.isdir(os.path.join(WorkingFolder,WithPosts)) == False:
    os.mkdir(os.path.join(WorkingFolder,WithPosts))

PostFiles = glob.glob(os.path.join("*","post.txt"))
for Folder in PostFiles:
   os.rename(os.path.join(WorkingFolder,os.path.dirname(Folder)),os.path.join(WorkingFolder,WithPosts,os.path.dirname(Folder)))

save above in a text file ane name it <whatever>.py
Put the file in the folder you wanna sort and run it.
Any folders with post.txt will be moved to the WithPosts folder, which you can change.
 
  • Like
Reactions: Gon
Status
Not open for further replies.
Back
Top