Long story short, I accidentally wiped my HDD the other day while trying to install a new SSD. I used a great program to undelete my partition; however, all the files were given an .efs extension. I am currently using:
:begin
RENAME *.extension.efs *.
I have tons of lines written for each file type, and this command works flawlessly. The only problem is that I have to manually paste this .bat into every folder and execute it in order for it to work.
Is there a way I can make it so when I run this .bat, it will go through all folders and subdirectories from a central directory? I'm anal about organization so all my music, albums, videos, TV shows, etc., are all in separate folders and it would take quite some time for me to run my original .bat from each.
Any help is appreciated!!
for /r /d %n in (.) do pushd "%n"&call "fullpathtoyourniftybatchfile"&popd
from the prompt will traverse the entire tree from wherever your current directory is.
You could also place your batch into any directory on your path and execute
for /r /d %n in (.) do pushd "%n"&call "yourniftybatchfilename"&popd
since windows searches the path for any executable it can't find in the current directory.
Here is a script that will rename files with extension efs in the current directory and all subfolders starting at the current directory. %%i is replaced by the full path to a folder or subfolders. Line 2 is needed because the for loop only does folders and subfolders.
#echo off
ren *.efs *.
for /f "tokens=* usebackq" %%i in (`dir/b/ad/s`) do (
cd %%i
ren *.efs *.
)
Related
I need to delete specific files from 28 folders on the same server.
e.g
C:/folder/DMP/app_x0
C:/folder/DMP/app_x1
C:/folder/DMP/app_x2
DeleteList.txt has a list of files names (with path).
C:/folder/DMP/app_x0/ABC1.txt
C:/folder/DMP/app_x0/ABC1.doc
The batch file needs to have a loop to go through each folder one by one and delete all files mentioned in a text file. Following worked ok for one folder only if I specify the full path before each file's names in DeleteList.txt file.
for /f "delims=" %%f in (DeleteList.txt) do del "%%f"
How to use above so that same code could run 28 times in batch file but each time replaces folder location path. DeleteList.txt will not change.
Any sample code/suggestion would help.
Thx.
One of these questions where we need to read between the lines.
Given we have on file of full filenames with the incorrect path separator, and a requirement to delete the files from 28 directories (but there's no list) then I conclude that since the required subdirectories shown are all subdirectories of C:\folder\DMP\ then the requirement actually is to delete all of the files that match the filenames in the file in all subdirectories of the parent directory of the full filename in the list.
So my suggestion would be
for /f "delims=" %%b in (DeleteList.txt) do del /s "%%~dpb..\%%~nxb"
Naturally, you should test this on a dummy tree before implementing it.
del /s deletes in the target directory and all subdirectories. The target subdirectory is the drive and path of %%b; its parent (..); the name and extension part of %%b.
I prefer to use metavariables that are not available as metavariable-modifiers.
To delete in subdirectories but not in the parent:
for /f "delims=" %%b in (DeleteList.txt) do FOR /d %%c IN ("%%~dpb..\*") DO del /s "%%c\%%~nxb">nul
This time, %%c is set to all subdirectorynames in turn, and the del/s applied to each subdirectory. The /s (...and subdirectories) probably isn't required. The >nul suppresses the deletion report if desired.
Not sure if the OP has need for this anymore, but you can use the following batch script to delete the specific files from multiple folders, starting from a parent folder.
#echo off
for /r "Drive:\path\to\parent_folder" %%d in (.) do (
del "%%~d\ABC1.txt"
del "%%~d\ABC1.doc"
)
The script will loop through the parent directory and its sub-folders, and delete only the specific files from the folders that they appear.
In the case of the post, the parent folder can be "C:\folder".
for /L %%f in (1,1,10) do copy File1.txt %%f.txt
this code does the job very well, but I'm trying to understand how to change it to make it read
subfolders, so that I don't have to keep moving the batch file to every folder
I saw this, but not really sure how to put it together
#echo off
SET "sourcedir=C:\Users\user\Desktop\Main\Original"
SET "destdir=C:\Users\user\Desktop\Main\Copied"
for /L %%f in ('dir /s 1,1,10) do copy *.txt %%f.txt
in the section - copy *.txt %%f - I put a * so that it can only look for .txt files, but this action
slows down the coping and then stops working.
I know my code is a mess, just trying to put something together
I have many Subfolders and each folder has 1 txt file in it with all random names
and I need to make multiple copies of each file.txt in each folder
I have so many subfolders that it would literally take me months of time to get all files copied
and by then I would have many more new files to work on
so getting this copier to read Subfolders is like top priority for me.
I would like help putting this together and then explaining how it links
because I'm interested in applying the Set and dir to other batch file I have
Please any details on this will be much appreciated
I was told to look into xcopy and robocopy, but I have no idea were to add a counter
#echo off
for /1 %f in (1,1,10) do xcopy "C:\Sources" "C:\Target" /c /d /i /y
exit
So I have this that reads from source and dumps in main folder where the batch is
Option 1)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\*.txt" %%f.txt
Option 2)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\" "C:\Destination Address\ %%f.txt"
The thing I don't like is that is asks me a question
and I have over 10,000 txt files, I can't sit here and press F for Filename
10,000 times, can we disable that
OK, so I got this working
all I need help with is were to add this /c /d /i /y
I am still trying to get it to read Subfolders with the batch sitting
in the main folder and me not having to move files back and forth
Option 3)
for /L %%f in (1,1,110) do copy "C:\Source Address\*.txt" %%f.txt`
This works well with the Source and the wild card #magoo told me to add
the Source before the .txt file
But with this code I would still have to open hundreds of folders and move
the file to the source run the copier and then move back all copied files
Still need help with Subfolders
for /L %%f in (1,1,10) do copy File1.txt %%f.txt
will vary %%f from 1 (the first number in the parenthesised list) to 10 (the last) in steps of 1 (the middle) and will therefore copy file1.txt to 10 separate files, 1.txt to 10.txt.
Since there are no paths specified, the copy will be performed from file1.txt in the current directory to 1.txt .. 10.txt in the current directory.
If you were to put your batch file in any directory that is mentioned in the path variable (just execute path at the prompt to show it) then no matter where the current directory is, you could just use that batch filename to execute the batch, and the files would be created by copying file1.txt in the now-current directory to 1.txt .. 10.txt in the now-current directory - if file1.txt exists in the now-current directory, and if not, it will generate error messages reporting that the source file is missing.
If you were to replace file1.txt in the batch with "x:\wherever\file1.txt" then the file would be copied specifically from the file "x:\wherever\file1.txt" regardless of whether file1.txt exists in the now-current directory. (And please get used to "quoting file or pathnames" as it will avoid a whole slough of problems when you tackle names containing spaces and some other special characters).
I have no idea how for /L %%f in ('dir /s 1,1,10) do is supposed to work since according to Hoyle, the first element in the parenthesised list should be a number. I'd suggest that we have a small transcription problem here.
Th slower and slower problem - yes, understandable. You are creating more and more .txt files, and copying all of them to a new destination...
Perhaps referring to This response and question might show how to solve your subdirectory-scan issue.
I need a batch file that will search all subfolders in a folder, find any folder named Reports (there will be many and file paths will be changing constantly) and then copy the reports folders contents (not the folder) back to a root directory for export. For example: I have a folder on my desktop called Cases. I need to search all sub folders for folders called Reports and then copy those files out.
Any help would be appreciated.
Here is a way you can do it (assuming you are talking about Cases folder in your desktop [in cmd]):
for /R "%userprofile%\Desktop\Cases" /D %A in (Reports.?) do #xcopy /s %~fA full_path_you_want
For batch file you should double the percent-signs (%%) just like this:
#echo off
for /R "%userprofile%\Desktop\Cases" /D %%A in (Reports.?) do xcopy /s %%~fA full_path_you_want
To learn more about commands and wildcards (.?) you can:
Type for /? in a fresh cmd
Check link https://www.robvanderwoude.com/battech_wildcards.php
Hope this helps!
I had a batch script that rename files inside the folder which looked like this:
ren B:\Backups\*.bc_ *.bc
Now I have files between many folders, the back up creates a new folder with a new name every day, and i need to rename files across several folders.
How can I do it? How to correctly use wild card in this case?
You cannot use a wildcard in the path in your REN statement. You will have to use some form of the FOR command.
Suppose you want to rename all *.bc_ files in the entire folder hierarchy rooted at B:\Backups.
You could use FOR /R to iterate all .bc_ files within the hierarchy and rename each file individually.
for /r "B:\Backups" %%F in (*.bc_) do ren "%%F" "%%~nF.bc"
Or you could use FOR /D /R to iterate all the folders under the root and run your wildcard REN against each of the folders
for /d /r "B:\Backups" %%F in (.) do ren "%%F\*.bc_" *.bc
Both of the commands above are designed to be used in a batch script. Change each double percent into a single percent if you want to run the command from the command line instead of from within a batch file.
I want to create a .bat file that will help me to find/search a particular .exe file in folders in a particular location and then run that .exe file.
e.g
search file in particular location e.g."\G:\data" and in this prime location there is many folders and in that folders many sub folders are there. and the .exe file is available in one of these sub-folder and the location I do not know. only I know the name of the .exe file.
bat file need to search that file and then run that one.
below two are not able to find and run the file.
1st program
pushd G:\data-a\test
FOR /F "delims=" %F IN ('dir /S /b autorun.exe') DO SET ExePath="%F"
%ExePath%e
2nd program
for /r G:\data-a\test %a in (autorun.exe) do "%a"
from the above two different program I put it in to notepad for creating two different .bat file and executed one after anther file. but that was unable to locate and run autorun.exe file.
just for example my autorun.exe file exact locations are
G:\data-a\test\test1\t1 and G:\data-a\test\test1\t1
but in G:\data-a\test location there may be several subfolders.
so to run autorun.exe one by one that to find the file in subfolder is difficult. I want to give the path G:\data-a\test in program and all the files will be found in subfolder and then run automatically.
Three liner (use %%F if wrapping inside a batch file).
pushd G:\data
FOR /F "delims=" %F IN ('dir /S /b something.exe') DO SET ExePath="%F"
%ExePath%
or the one liner from the command prompt:
for /r g:\data %a in (filename.exe) do "%a"
used the quotes for %a in case the calling directory path has spaces in it