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!
Related
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 *.
)
I need to be able to create a bat script to do 3 things:
Search for multiple specific filenames in a directory.
Find the most recently generated version based on each filename specified.
Copy that most file to a new dir.
I am very new to coding in general, so any assistance would be much appreciated.
So far all I have been able to do is figure out how to copy files from one location to another using the below:
xcopy /s c:\source\differentfilename1.csv d:\target\
xcopy /s c:\source\differentfilename2.txt d:\target
xcopy /s c:\source\differentfilename3.html d:\target
So far I have tried the following and its not copying the files over:
ECHO
CD D:\Data\
MKDIR D:\Data\CopyFilesHere
for /R %file in (Filename1.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename2.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename3.*) DO XCOPY "%file" D:\Data\CopyFilesHere
I have since noted there are subfolders I need to search through also.
In a folder, I have some folders. I want to compress all the folders separately to the foldername.rar and delete the original files. I want to perform this function in batch.
I tried the ones given in other answers but they only compress the files if present, or do nothing. Here , I have to compress only folders to their respective archive. Please help
WinRAR includes two command-line tools, rar.exe and unrar.exe, where rar.exe compresses and unrar.exe uncompresses files.
Both are located in the “C:\Program Files\WinRAR” folder in the installable version.
Assuming, if there are multiple folders under D:\test and you want each folder to get its own .rar file , in the parent folder, from a batch file, this works for you:
#echo off
setlocal
set zip="C:\Program Files\WinRAR\rar.exe" a -r -u -df
dir D:\test /ad /s /b > D:\test\folders.txt
for /f %%f in (D:\test\folders.txt) do if not exist D:\test\%%~nf.rar %zip% D:\test \%%~nf.rar %%f
endlocal
exit
Explanation....
It'll create .rar files of all the folders/subfolders under parent folder D:\test in the same parent folder.
Then, it'll delete all the original folders/subfolders under parent folder D:\test and thus you'll be left only with the archives at the same place.
“a” command adds to the archive
“-r” switch recurses subfolders
“-u” switch. Equivalent to the “u” command when combined with the “a” command. Adds new files and updates older versions of the files already in the archive
“-df” switch deletes files after they are moved to the archive
Maybe something like this, change C:\testfolder\ to you liking.
In my example I had 3 folders (with random files and subfolders inside em): one, two and three
#echo off
cd "C:\testfolder\"
for /d %%G in ("*") do (
"%programfiles%\WinRAR\Rar.exe" a -r %%G.rar %%G
rd /s /q %%G
)
How do you copy specific file types from one folder into another folder while retaining the folder structure?
The following batch command is capable of copying the specific file types to a folder, but lacks being able to retain the folder structure:
for /R c:\source %%f in (*.cpp,*.h) do copy %%f x:\destination\
How could I modify this to keep the folder structure from the source? Thanks!
xcopy /e c:\source\*.xml x:\destination\
should do the job.
See
xcopy /?
from the prompt for documentation. Use /e for with-empty-directories or /s for to ignore empty directories.
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.