How to replace entire dir to destination dir and sub-dirs? - windows

Just want to ask, how can I replace files in destination directory and subdirectories with the same files from source? Say I want to replace file "X" in destination directory or subdirectory with file "X" from source directory, the script must go through all directories and subdirectories to find file "X" and replace it
Example
Source: "C:\MyPics\New\*.*"
Destination: "C:\MyPics\All\A\*.*"
"C:\MyPics\All\B\1\*.*"
"C:\MyPics\All\B\2\*.*"
"C:\MyPics\All\C\*.*"
Do you have any suggestions to accomplish this?
Hope to hear from you soon.
Thanks

This is untested, but I think I've got the code correct.
If you are looking to replace with a single file from your source, then
#echo off
set "src=C:\MyPics\New\"
set "dst=C:\MyPics\All\"
set "file=X"
for /f "eol=: delims=" %%F in ('dir /b /s "%dst%%file%"') do copy /y "%src%%file%" "%%F"
If you are looking to replace with all files from source, then
#echo off
set "src=C:\MyPics\New\*"
set "dst=C:\MyPics\All\"
for %%S in ("%src%") do (
for /f "eol=: delims=" %%F in ('dir /b /s "%dst%%%~nxF"') do copy /y "%%S" "%%F"
)
You could change the mask in the src definition to be more specific then *

Related

search the requested file in subfolders and copy to destination

I am trying to copy the listed files in file_list.txt to destination folder from source location, source folder has subfolders. my batch should be capable to search the file in source subfolders and copy to destination folder. same for copy all files with extension .exe. what is wrong with my code. I think, I have missed to search subfolders data. don't know what is the command. please help.
#Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
#color 0a
cls
set "dest=D:\destination"
set /p source=Select source path:
for /R %%f in (%source%/*.exe) do copy %%f "%dest%"
echo Copy requested files
for /f %%f in (file_list.txt) do (
for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%source%/%%f"') Do (
copy "%%F" "%dest%\%USERNAME%"
)
)
pause
ENDLOCAL
I cannot see anything obviously wrong with the code you've posted, although there are possibilities for errors. You have not provided sufficient information, about your current directory, the content of the text file, how the script is invoked or any debugging information.
The following version of your code, requires that you put your file listing text file into the variable definition on line five. I've assumed that the batch file is in the same location as the file listing, and therefore used %~dp0. If it is in the current directory instead, then replace that with %CD%\, and obviously the fully qualified absolute path if neither.
Next I use some validation to try to ensure that the source, destination, and user input exist. The input location will then become the current directory.
Your provided commands are then run, before the curent directory is returned to its original location.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Color 0A
Set "listdir=%~dp0filelist.txt"
Set "dest=D:\destination"
If Not Exist "%listdir%" GoTo :EOF
If Not Exist "%dest%\" GoTo :EOF
:Ask
ClS
Set "source="
Set /P "source=Select source path: "
If Not Defined source GoTo Ask
PushD "%source:"=%" 2> NUL || GoTo Ask
For /R %%G In (*.exe) Do Copy /Y "%%G" "%dest%"
Echo Copy requested files
For /F "UseBackQ EOL=? Delims=" %%G In ("%listdir%"
) Do For /F "Delims=" %%H In ('Dir "%%~nxG" /S /B /A:-D 2^> NUL'
) Do Copy /Y "%%H" "%dest%\%UserName%"
PopD
Pause
Feel free to try the code, and provide some proper debugging information if it still fails to work as intended.

Moving files from Subfolders into root directory, but not copying them to the next directory when ran again

I have a folder that contains subfolders with MP4 files. I'm trying to write a script that will move the MP4 files out of the subfolders into the root folder when ran. The batch file I wrote is working, but when the batch script runs again for new subfolders, the MP4 files that were already copied to the root folder, get moved up another level in the file structure. For example:
C:\MainRoot\Root\Subfolder\media.mp4
When script is ran, 'media.mp4' gets moved up to C:\Root\media.mp4 as desired.
But since I need the script to run on a scheduled task. The next time the script runs I get the following:
C:\MainRoot\media.mp4
Instead of just the MP4 file staying in C:\MainRoot\Root.
Here's my batch file so far to copy the mp4 files:
set root_folder=C:\MainRoot\Root
for /f "tokens=1* delims=" %%G in ('dir %root_folder% /b /o:-n /s ^| findstr /i ".mp4" ') do (
move /y "%%G" "%%~dpG..\%%~nxG"
)
What do I need to modify so that once moved, the MP4 files will stay in place?
Any help would be greatly appreciated!
Since all your source files seem to be at a certain directory level, a for /D loop could be wrapped around your for /F loop, which parses the output of a non-recursive dir command line (no /S):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=C:\MainRoot\Root"
set "_PATTERN=*.mp4"
rem // Loop through sub-directories:
for /D %%D in ("%_ROOT%\*") do (
rem // Loop through matching files:
for /F "eol=| delims=" %%F in ('dir /B "%%~fD\%_PATTERN%"') do (
rem // Avoid overwriting destination file:
if not exist "%_ROOT%\%%~nxF" (
rem // Move matching file one level up:
move /Y "%%~fD\%%~nxF" "%_ROOT%\%%~nxF"
)
)
)
endlocal
exit /B
If you are happy to overwrite as in your provided example then something as simple as this may suit your purpose:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /R %%G In (*.mp4) Do If /I NOT "%~dpG"=="%root_folder%\" Move "%%G">Nul 2>&1
If the files are only one folder deep you may prefer this:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /D %%G In (*) Do Move "%%G\*.mp4">Nul 2>&1

Adding Parent Directory name as Prefix to sub directory files using batch file

Adding Parent Directory name as Prefix to sub directory files using batch file
I need to add the prefix of the parent directory to all the files present in sub directories
for example we receive html and text files within Directories 101 as parent directory and Creatives as sub directory
F:\Files\101\Creatives\filename.htm and filename.txt
F:\Files\102\Creatives\filename.htm and filename.txt
F:\Files\103\Creatives\filename.htm and filename.txt
here I want to omit the sub directory name (Creatives) and add 101_, 102_, 103_ as the prefix to the filenames present in sub directories
example 101_filename.htm also for the text file as 101_filename.txt in sub folders
with the below code I can add the prefix to the sub folder files but I can only replace it with a static value (Prefix_), here I need to add the first directory name(101, 102, etc) as prefix to all the files present in (F:\Files) folder
#echo off
pushd "F:\Files"
for /r %%j in (*) do (
rename "%%j" "Prefix_%%~nxj"
)
popd
#echo off
pushd "F:\Files"
for /d %%P in (*) do for /f "delims=" %%F in ('dir /b /s /a-d "%%P"') do rename "%%F" "%%P_%%~nxF"
popd
The inner loop is FOR /F with DIR command instead of FOR /R to eliminate possibility of loop renaming the same file twice.
I guess you could find a solution involving 2 for loop.
Something like
for /f "tokens=*" %%d in ('dir /b') do ( for /f "tokens=*" %%f in ('dir /s /b %%d') do ( rename "%%f" "%%d_%%~nxf" ) )
Best Regards,
Michel.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "source=u:\Files"
pushd "%source%"
for /r %%j in (*) do (
SET "prefix=%%~dpj"
CALL :setpfx
ECHO rename "%%j" "!Prefix!_%%~nxj"
)
popd
GOTO :EOF
:setpfx
SET prefix=!prefix:*%source%=!
SET prefix=%prefix:\=.%
FOR /f %%t IN ("%prefix%") DO SET prefix=%%~nt
SET prefix=%prefix:.=_%
SET "prefix=%prefix:~1%"
GOTO :eof
This may do what you require. The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO RENAME to REN to actually rename the files.

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.

List only files from subfolders of specified folder

This question might have a simple solution, but I just cannot find it.
Let's say I'm using the DIR command to produce a list of .txt files in the folder "E:\Documents". How can I make sure that only the .txt files one level below "E:\Documents" are listed (i.e. in a direct subfolder) and not the files in "E:\Documents" itself. "E:\Documents\\" or "E:\Documents\*\" doesn't seem to do the trick.
Thanks in advance!
FOR /f "delims=" %%i IN ('dir /b/ad') DO IF EXIST ".\%%i\*.txt" DIR ".\%%i\*.txt"
That's if you want the output in DIR format - with headers
FOR /f "delims=" %%i IN ('dir /b /ad') DO IF EXIST ".\%%i\*.txt" (
FOR /f "delims=" %%q IN ('DIR /b ".\%%i\*.txt" ') DO ECHO ".\%%i\%%q"
)
if you want just the filenames.
This can be achieved without dir:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~ff
If you need not only the file names, but also detail information about the files, add the respective qualifiers, e.g.:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~azff
a → attributes of the file
t → timestamp of the file
z → size of the file
See help for for details.

Resources