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

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.

Related

Windows command to search directory for a specific name, then move its files to parent

I've been trying to get a command to work:
for /r /d %d in (*) do if /I "%~nxd"=="_Success" for /f %f in (%d\*) do if exist %f move %f ..
I'm intending the command to do the following:
Descend the directory tree and find directories that are named "_Success"
Move files (if they exist) to the parent directory.
I have been using help and the follow threads to get me started:
How to move all files with specific extension from all subdirectories to their parent using CMD?
Batch Script to Find a Folder inside Sub Folders and get Folder Path
However I'm getting a few errors back in my output:
C:\migration_files\vault>if /I "document" == "_Success" for /F %f in (C:\migration_files\vault\document\*) do if exist %f move %f ..
...
C:\migration_files\vault>if /I "_Success" == "_Success" for /F %f in (C:\migration_files\vault\faqs\_Success\*) do if exist %f move %f ..
The system cannot find the file C:\migration_files\vault\faqs\_Success\*.
C:\migration_files\vault>if /I "_Success" == "_Success" for /F %f in (C:\migration_files\vault\patch\_Success\*) do if exist %f move %f ..
The system cannot find the file C:\migration_files\vault\patch\_Success\*.
EDIT -
It seems to mostly working except that the file is being moved to the working directory.
Thanks to #mihai_mandis with his solution at this link:
batch moving file from subfolder to parent folder
I modified it slightly to the following:
#echo off
setlocal enabledelayedexpansion
for /r /d %%d in (*) do (
if /I "%%~nxd"=="_Success" (
for /f %%f in ("%%d\*") do (
call :GETPARENTPARENT "%%f" ret
echo ret=!ret!
move /Y "%%f" "!ret!"
)
)
)
goto:EOF
:GETPARENTPARENT
set fileP=%1
echo received=%fileP%
for %%a in (%fileP%) do (
set parent=%%~dpa
cd !parent!\..
set PPPath=!cd!
for %%x in ("!PPPath!") do (
set "%~2=%%~dpnx"
)
)
GOTO:EOF

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

Bat to delete specific folders in subfolders listed in a text file

Bat to delete specific lis of subfolders
In windows xp I have a folder name gaming that contain 100 folders with names games1, games2, games3 ... games100.
And inside every of those folders there is a huge list of subfolders from 1 to 100000.
And I have a name list of the 50000 folders that i want to delete
example of the name list that I want to remove without knowing the name of their parent directory
6383
6385
4850
6395
6396
6397
9865
6401
6408
1200
...
..
.
try this:
#echo OFF &SETLOCAL
SET "namelist=list.txt"
for /f "usebackq delims=" %%a in ("%namelist%") do set "$%%a=1"
for /d /r "gaming" %%a in (*) DO IF DEFINED $%%~na ECHO rd /s /q "%%~a"
Look at the output and remove the word echo before rd if it looks good.
This expects a list.txt in the d:\folder\gaming folder and it will create "removefolders.bat.txt" in the same folder.
Open that file in Notepad and verify that the correct folders are listed and then you can rename it to .bat and run it to actually delete the folders.
#echo OFF
pushd "d:\folder\gaming"
del "removefolders.bat.txt" 2>nul
for /f "delims=" %%a in ('type "list.txt" ') do (
for /d /r %%b in (*) do if "%%~nxb"=="%%a" >>"removefolders.bat.txt" echo rd /q /s "%%b"
)
popd

Renaming files by folder hierarchy

I have many files with the following structure:
1969/ar/1.jpg
1969/ar/2.jpg
1969/he/1.jpg
1969/he/2.jpg
1969/en/1.jpg
1969/en/2.jpg
1970/ar/1.jpg
etc...
I want to rename all of them, with one command, to one directory, while their names reflect their original folder location.
1969_ar_1.jpg
1969_ar_2.jpg
1969_he_1.jpg
1969_he_2.jpg
1969_en_1.jpg
1969_en_2.jpg
1970_ar_1.jpg
etc...
Is it possible to do so with one command or a batch file?
Thanks!
You may do that to move the files to the base folder with this command-line:
for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_!
Execute it from the folder that contain the 1969, 1970... folders. IMPORTANT!: Delayed Expansion must be active in order for this line to work, so you must previously activate it executing cmd.exe with /V switch this way: cmd /V.
For example:
>xcopy test backup /s
test\1969\ar\1.jpg
test\1969\ar\2.jpg
test\1969\en\1.jpg
test\1969\en\2.jpg
test\1969\he\1.jpg
test\1969\he\2.jpg
test\1970\ar\1.jpg
7 File(s) copied
>cd test
>dir /B
1969
1970
>for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" !f:\=_!
>dir /B
1969
1969_ar_1.jpg
1969_ar_2.jpg
1969_en_1.jpg
1969_en_2.jpg
1969_he_1.jpg
1969_he_2.jpg
1970
1970_ar_1.jpg
Modify the line this way to move the files to another folder:
for /R %a in (*) do #set f=%a& set f=!f:%cd%\=!& move "%a" "\other\folder\!f:\=_!"
Or via this Batch file:
#echo off
setlocal EnableDelayedExpansion
for /R %%a in (*) do set f=%%a& set f=!f:%cd%\=!& move "%%a" "\other\folder\!f:\=_!"
Run this from the base of the tree that contains all *.jpg files.
Change the target folder to where you want the files to go:
Test it on some samples first.
#echo off
for /f "delims=" %%z in ('dir "*.jpg" /b /s /a-d ') do (
for %%a in ("%%~dpz%\.") do (
for %%b in ("%%~dpa\.") do (
ren "%%z" "%%~nxb_%%~nxa_%%~nxz"
move "%%~dpz\%%~nxb_%%~nxa_%%~nxz" "c:\target\folder"
)
)
)
pause
try this (look at the output and remove the word echo before move, if it is OK):
#echo off &setlocal
for /d %%i in (19* 20*) do (
cmd /c "for /r "%%i" %%j in (*.jpg) do #for %%k in ("%%~dpj.") do #echo move "%%~j" "%%i_%%~nk_%%~nxj""
)

how to traverse specified subfolders in a windows batch file?

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.

Resources