how to traverse specified subfolders in a windows batch file? - for-loop

Here is my folder hierarchy:
[Numbers]
[Numbers/12545]
[Numbers/12545/dev]
[Numbers/12545/prod]
[Numbers/32445]
[Numbers/32445/dev]
[Numbers/32445/prod]
...
[Numbers/.....]
[Numbers/...../dev]
[Numbers/...../prod]
I want to copy some text files under the only "[Numbers/...../dev]" folders. How should i do?
I tried the below code and it's not work because it coppies under the all subfolders.
for /r %NUMBER_DIRS% %%d in (.) do (
copy %PROJECT_INPUTS%\*.txt "%%d"
)
Thanks.

Try this:
for /d /r "%NUMBER_DIRS%" %%d in (*DEV) do copy "%PROJECT_INPUTS%\*.txt" "%%~d\*.txt"

#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (
'dir /s /b /a:d "\numbers" ^| findstr /i /e "\dev"'
) do ECHO COPY %PROJECT_INPUTS%\*.txt "%%i\"
This will report what the batch PROPOSES to do. Remove the ECHO keyword before the COPY to execute the copy.
Note : you may need to add /y to the copy options if you want to OVERWRITE an existing file in the destination directories.
I presume that you're copying FROM %PROJECT_INPUTS% TO many ...\dev directories.

Related

Copy and rename multiple files in multiple directories with windows CMD line

I want to copy and rename files that are stored in a stack of directories (year, month, day). The files are .ers files and I need them to maintain their extension but to go from file.ers to file_cloud.ers.
Then I would like the files for a given month to be saved in the cloud folder within each month.
Ideally I would do this in the Windows Command Line.
I understand a batch file is a good place to start, and so I have the following - this doesn't include saving in a different location - but I am struggling with just the copy and rename.
for /D %%y in ("C:\Data\20*") do (
pushd "%%~y"
for /D %%m in ("*") do (
pushd "%%~m"
for /D %%d in ("*") do (
pushd "%%~d"
for %%f in (soig_*.ers) do copy "%%~f.ers" "%%~f_cloud.ers"
popd
)
popd
)
popd
)
This might be easier than you are making it. If this echos the correct copy command, remove the echo. You get this by sticking your head way down into the output of FOR /?.
FOR /F "usebackq tokens=*" %%f IN (`DIR /S /B /A:-D "*.ers"`) DO (
echo COPY "%%~f" "%%~dpnf_cloud.%%~xf"
)
The following should work okay for you, (it is supposed to copy all matching .ers files with updated names to the existing cloud directory at the previous level of their tree).
#FOR /D %%Y IN ("C:\Data\20*") DO #FOR /D %%M IN ("%%Y\*"
) DO #FOR /D %%D IN ("%%M\*") DO #IF /I NOT "%%~nxD"=="cloud" FOR %%F IN (
"%%~D\soig_*.ers") DO #COPY "%%F" "%%~M\cloud\%%~nFcloud%%~xF">NUL
I have not included anything to prevent overwrites of files whose names already exist within that cloud directory

batch file copy from subdirectories and rename

Problem: I am trying to copy *.pdf from multiple subdirectories. These *.pdf can have the same name and i need to rename them. I also needed to copy the *.pdf by date from January to present but i was just trying to get the copy to work first.
My Attempt:
pushd "S:\Clients\"
for /f "delims=" %%f in ('dir S:\Clients\*.pdf /s /b') do (
if exist "C:\Users\administrator\Desktop\Jan-March\%%f" ( xcopy "S:\Clients\%%f" "C:\Users\administrator\Desktop\Jan-March\%%f" 'dir /s /d )
)
popd
pause
Thank you for any help in advance!

Batch copying to all folders

I need to batch copy two folders, let's call them A and B, from F:\Sourcefolder\ to F:\destinationfolder subfolders (not to destination folder itself).
Now I know when batch copying file (file.exe for example) it is supposed to look something like this
for /r "F:\destinationfolder" %%i in (.) do #copy "F:\Sourcefolder\file.exe" "%i"
In each of those subfolders there is a lot of files. After copying A and B folders to all subfolders, I would like to move all files within the subfolders to folder A within their folder. Is this possible to do?
the XCOPY command is designed for folder copy, FOR /D will list level1 folders:
for /d %%a in ("F:\destinationfolder\*") do (
XCOPY "F:\Sourcefolder\A\*" "%%~fa" /s /i
XCOPY "F:\Sourcefolder\B\*" "%%~fa" /s /i
)
for recursive copy (all subfolders):
for /r /d "F:\destinationfolder\" %%a in (*) do (
XCOPY "F:\Sourcefolder\A\*" "%%~fa" /s /i
XCOPY "F:\Sourcefolder\B\*" "%%~fa" /s /i
)
FOR /R will not work properly if there's no wildcard in the brackets - ? or *
ROBOCOPY,XCOPY

how can I batch copy a file into multiple sub-directories?

I have a directory called Case Files, and within it many directories labled:
Defendant 1
Defendant x
Defendant 88
Defendant !!
etc...
(btw these folders are the defendants names, they're not actually labeled 'defendant')
In each of the Defendant folders is a directory called Pleadings. Is there a way I can copy filexxxx.pdf into the Pleadings folder for each defendant? Preferably I would like to just run a simple .bat if possible.
This should copy the pdf into every folder called pleadings under c:\case files
#echo off
for /d /r "c:\case files" %%a in (*) do (
if /i "%%~nxa"=="pleadings" copy /y "c:\folder\file.pdf" "%%a"
)
pause
#echo off
cd /d "c:\case files"
for /r %%d in (~) do (
echo %%d | find /i "\Pleadings\~" >nul 2>&1 && copy /y "c:\filexxxx.pdf" "%%~dpd\"
)
Try this from within the Case Files directory:
FOR /F "usebackq delims==" %i IN (`dir /s /b Pleadings`) do copy /y filexxxx.pdf %i
But check my comment because I have a feeling that's not exactly what you're looking for.

Copy the newest file from all the subdirectories

I am trying to get all the newest file from subdirectories into one network directory with a command without getting the subdirectory structures. They are the SQL Server log files with extension of *.trn. I have the following but it doesn't work.
Trying to get only the newest *.trn files from ...........Backup and it's subdirectories.
for /R E:\SQLSERVER\PRODINSTANCE1\Backup %%f in (*.trn) do xcopy %%f "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
You can use the dir command with the /od switch and a for loop:
#echo off &setlocal enabledelayedexpansion
for /d /r "E:\SQLSERVER\PRODINSTANCE1\Backup" %%a in (*) do (
for /f "delims=" %%i in ('dir /b /a-d /od "%%~a"') do set "newest=%%~fi"
xcopy "!newest!" "\\198.152.71.14\NetBackups$\MSSQL\Logs" /B /O:D /d /Y
)
For more help enter help dir in the command prompt.
I'm not sure if you're using xcopy correctly. /O doesn't take arguments. It only copies ownership/ACL info. (Is that really what you want for log files?)
Since you didn't describe what "doesn't work" means, my only suggestion is to hedge against file names with spaces in them.
FOR /R "%SRC_DIR%" %%f in (*.trn) do xcopy "%%~f" "%DEST_DIR%" /B /O /D /Y
This worked for me (I tested with .pdf's).

Resources