Script to change the folder name for 10,000 folders in windows - windows

I want to change the folder name for 10,000 folders in windows.
I have old structure available and I want to change it to new one. For example:
Old structure: C:\Documents to read\Important\Domain\Latest\Today
New structure: C:\Documents\Important\Domain\new\yesterday
Is there any way through which I can change the 10,000 flies by providing folder structure in a batch file?
(old folder structure to new folder structure without impacting the underneath files)
I tried - ren "old file name" "new file name" in command prompt but it is very specific to particular folder.
Please help. I am very new to the command prompt. Any kind of help is very much appreciated.
Thank you!

To rename a folder you have to use mv /home/user/oldname /home/user/newname
after this you have to use a for loop for all your folder

Windows has limited batch file capability, so what I've done is:
DIR /S /AD /B > files.tsv to output all the files in bare full format
import files.tsv into Excel
new column with a formula to generate "after"
new column with a formula to create a REN command, e.g. ="REN """ & A1 & """ """ & B1 & """"
paste that column into a batch file DOIT.BAT
very important - backup all the files! very easy to mess up
run DOIT.BAT
DIR /S /AD /B does recurse into all subdirectories, which is annoying, but it does list them in hierarchical order, so it might be easy to edit out the ones you don't want.
The extra quotes are to make sure spaces in directories and file names for REN are handled properly.

Related

get file name of directory and subdirectory using CMD

I am making an inventory for which I need to copy all file names in an excel sheet. Right now I am using cmd command "dir /s /w /p > file.txt" for every folder. Problem is that when I get the txt files, file names are in same line, and I have to select them and copy paste in Excel sheet. I need a command that gives me all files names should come in new line so that I can easily copy all file names at once and paste them.
You are getting file names in the same line because you use the /w (wide) option, which does exactly this, check /w parameter here https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir
To solve your problem simple don't use the /w parameter. You can see the result below

Batch OCR files in subfolders and save new files with new name

I have the following code, which OCR's all PDF files in a specific folder (d:\extracttmp2), but it does not rename the files as I would like, or put the new files in the right place.
Currently, all files are within subfolders of 'extracttmp2'.
The OCR runs correctly, but I would like the OCR'ed files to be renamed to: <parent folder path>-<filename>_ocred.pdf. Naming them in such a manner will produce no file overwrites.
Currently, the code OCR's the files, but it saves the new files to the folder above the folder they are located in. It also saves the filenames as "JAN_ocred.pdf", for example, for a file named "JAN.pdf". The result of saving up one folder leads to some file overwrites, which is unwanted.
Also, it doesn't matter if the OCR'ed files remain in the folder where the un-OCR'ed files are located, or if they're saved up one folder. The desired renaming will eliminate any overwrites.
The software I'm using is PDF24. https://creator.pdf24.org/manual/10/#command-line. However, I think that my problem is not with the OCR software, but my syntax in the batch script.
Can anyone tell me what I am doing wrong?
For /R d:\extracttmp2\ %%G in (*.pdf) do "C:\Program Files\PDF24\pdf24-Ocr.exe" -outputFile "%%~nG_ocred.pdf" -language eng -dpi 300 -skipFilesWithText "%%G"
Is this what you mean? i.e. files will be saved in the same location as before, but each name will be prefixed with their parent directories' name, followed by a hyphen/dash.
#For /R "D:\extracttmp2" %%G In (*.pdf) Do #For %%H In ("%%~dpG.") Do #"%ProgramFiles%\PDF24\pdf24-Ocr.exe" -outputFile "%%~nxH-%%~nG_ocred%%~xG" -language eng -dpi 300 -skipFilesWithText "%%G"
Just a quick clarification: D:\extracttmp2\directory1\JAN.pdf would be saved in the working directory with the name directory1-JAN_ocred.pdf, and D:\extracttmp2\directory2\subdirectory3\SOMENAME.pdf, as subdirectory3-SOMENAME_ocred.pdf
If you want to save the files somewhere else, either change the working directory, or prepend it to %%~nxH-%%~nG_ocred%%~xG

Forfiles Command Windows cmd - forfiles which contain 2 or more string snippets in filename do something

I'm trying to echo a list of files which are named with multiple string snippets. I have a folder with subfolders which contain PDFs named like:
EDW_L1_C3.pdf
EDW_L1_C4.pdf
EDW_L2_C10.pdf
EDW_L5_C2.pdf
EDW_L5_C3.pdf
etc.
A crucial point is that I can't be certain that the order of filename codes is always the same. For instance, it could be that there are some files named:
C2_EDW_L5.pdf
For context or if anyone cares the names contain a township (EDW), Lot (L1_) and concession (C3_) code and the files are land surveys.
I want to create a batch file to list the files recursively which have BOTH L1_ AND C3_ strings in filename. For just L1_ I have used the following successfully:
forfiles /S /M *L1_*.PDF /c "cmd /c echo #FILE"
However I am stuck at how to narrow that list to only include L1_ files which also have C3_ in filename. ANYONE HAVE ANY IDEAS?
My end goal after I get this code to work is to copy those filtered files to a specified folder. I'm just playing with code at this point to create a custom solution.

Windows CMD or BATCH file that "flags" empty folders (and folders that only contain empty folders)

Most companies I have worked for start new projects with a templated folder structure. Windows will automatically flag empty folders (the icon shown in the file explorer is that of an empty folder) however, folders whose subfolders are also empty will not be flagged (their icon shows a folder containing files). This can create confusion and lead to mistakes during the early stages of a project as it would appear at a glance that some folders have had their content added when they really only contain empty subfolders.
So my question is:
How can I iterate over folders with empty subfolders and "flag" them via the use of a Batch file.
The .bat file would need to search through subfolders to determine if a folder has any real content. If the folder does not have real content then the .bat file would need to flag it (flagging could be done with a change of its icon or filename). This would make it much easier to navigate new projects with large templated folder structures.
I don't need a completed file, I would just like to know how it could be achieved. However, any tips or suggestions on how to achieve this functionality would be more than welcome!
*Edit
Just to clarify I will show an example:
If I create an empty folder called 'Project' it will display with the Empty Folder icon. As shown Below:
Now I will add a new folder to my project folder called 'I am Empty':
The folder 'Project' no longer displays with the Empty Folder icon. It now uses the icon that shows it with content. As shown Below:
What I want is a .bat file that will parse the contents of the 'Project' folder and determine that it only contains the 'I am Empty' folder, which is empty and "flag" that folder (to flag it we could change the icon of the 'Project' folder back to the Empty Folder icon, change the name or "gray it out"). As shown Below:
cd example
for /d %%i in (*) do #dir /b /s /a-d "%%i">nul 2>&1|| #echo "%%i" has no files
give for an additional /r if you want to check subfolders too.
The trick is to list files only (/a-d) in the folder and all subfolders (/s) and if this fails (||) (because there are no files), do something with this folder (just echo here, but rd /s /q or ren is also possible)
Building on the answer given by Stephan and looking at other related stack exchange posts I pieced this solution together and it works well for my needs:
#ECHO OFF
PUSHD "%~dp0"
FOR /f "tokens=* delims=" %%F in ('dir /b/s/ad *') DO (
#dir /s /a-d-s "%%F">nul 2>&1|| ATTRIB +H "%%F"
#dir /s /a-d-s "%%F">nul 2>&1&& ATTRIB -H "%%F"
)
POPD
EXIT
I opted for a solution that did not modify file names. While testing that approach I realized it doesn't create the best user experience.
Instead, running this batch file hides the folders that are effectively empty and if your folder settings allow you to see hidden folders then they appear faded out. It also re-shows hidden files that have new content since the last time the batch file was ran.
For those of you coming to this solution who, like myself, are relatively inexperienced with batch scripting. I will explain what the code does and why (as I understand it).
I don't want to change the batch file for each implementation so I call PUSHD "%~dp0 to set the active directory to the folder containing the batch file (this lets me include the batch file in the folder-structure template, which is copied and pasted for each project)
Since I decided to use the hidden attribute for folder flagging, I needed to modify the FOR loop . FOR loops typically ignore hidden files which becomes troublesome if you need show a file that was previously hidden because it has new content. Running FOR /f "tokens=* delims=" %%F in ('dir /b/s/ad *') DO ()
allows the batch file to loop through all files mainly because of the /f attribute, but check out
this post about looping through hidden folders for more information.
Inside the for loop I am pretty much doing what Stephen suggested in his answer with the added logic to remove the hidden attribute on folders that no longer need it.
The only thing this batch file is lacking, is the ability to auto update on folder modifications or to auto-run on folder open (I hear this might be possible with an .ini file?) however, for my needs it will suffice to rerun the batch file, manually, after making changes to the folder.
Batch scripting is way outside of my comfort zone as far as scripting languages go so please forgive and correct me if I have made any mistakes or if there is a more reliable way to do what I need.

Windows batch script: move files that contain duplicate lines

Thank you in advance for any help.
I am requesting a batch script that I can run in windows xp.
This script should search a folder containing multiple files and look in each file to see if there are any duplicate lines in that file. And if there is, then that file would be moved into a new folder.
The script below I know works to move a string to a new folder:
FOR %%a in (C:\test*) do (findstr /C:"string" "%%a" >nul && move /Y "%%a" "C:\new folder")
This is about the extent of my knowledge when it comes to scripts.
Could I please have it built on to instead searching for a specific string, but to identify if there are duplicate lines? So it goes without saying that each line should be unique, otherwise I need to move the file to a new folder.
Thanks again for your assistance

Resources