xcopy batch issue - batch-file

I want to copy a set of subfolders where name contains items on a list. The list has a set of codes (e.g. ABC1, ABC2) but the folders are named ABC1_revised_2018, etc. My batch file I put together is below. What I am getting a '"Usebackq tokens=^" was unexpected' error.
#ECHO ON
SET FileList=C:\filelist.txt
SET Source=C:\Files
SET Destination=C:\Files-Parsed
FOR /D "USEBACKQ TOKENS=^" %%D IN ("%FileList%") DO XCOPY /E /F /D "%Source%\%%~D" "%Destination%\"
GOTO :EOF
I am attempting to use ^ to denote match beginning of string but that clearly isn't working. Any ideas? I have tried with a batch file and also line by line in cmd.
append
Folder
-ABC1-text-date (this is a subfolder)
-ABC2-text-date
filelist.txt only has values like ABC1, ABC2, etc. not exact matches does this help?

Well, if you want to recurse through directories and copy sub directories as per partial matches inside the file:
#echo off
set "FileList=C:\filelist.txt"
set "Source=C:\Files"
set "Destination=C:\Files-Parsed"
for /f "delims=" %%a in (%filelist%) do (
pushd %source%
for /f "delims=" %%i in ('dir /s /b /ad "%%a*"') do xcopy /E /F /D "%%~fi" "%Destination%"
popd
)
after getting the entry in the file, for /d will do a directory listing of the directory* in the source directory and physically copy the dir as C:\source\*\ABC2018 etc.

Related

Copy a file from one directory and replace the file in multiple directory

I need to copy a jar file from directory(source) and replace the file in the destination. But the problem is my destination directories are different as explained below:
Source=D:\temp\R56A
Target=D:\path\AP\Different_folders\lib\i2
Target folder example:-
D:\path\AP\ABC1\lib\i2
D:\path\AP\XY_C\lib\i2
D:\path\AP\GHS3\lib\i2
I AM NOT ABLE TO FETCH THROUGH DIFFERENT FOLDER NAMES and the script not taking it.
This is for a windows box. Can we copy the folder name in a text file and call that text file as variable in a for loop? Is it possible?
#ECHO OFF
REM SETLOCAL ENABLEDELAYEDEXPANSION
set Source=D:\temp\R56A
set Target=D:\path\AP\<Different_Directory_names>\lib\i2
set file=i2-bam.jar
for /f "delims=" %%f in ('dir /a-d /b /s "%Source%\%file%"') do (
copy /V "%%f" "%Target%\" 2>nul
)
PART 2
#ECHO OFF
for /d "D:\temp\R56A\" %%f in (i2-bam.jar) do copy %%f "D:\path\AP\<Different_Directory_names>\lib\i2"
Is this what you're trying to do?
#Echo Off
Set "Source=D:\temp\R56A"
Set "File=i2-bam.jar"
Set "Target=D:\path\AP"
Set "Sub=lib\i2"
If Not Exist "%Source%\%File%" Exit /B
If Not Exist "%Target%\" Exit /B
For /D %%A In ("%Target%\*")Do If Exist "%%A\%Sub%\" Copy /Y "%Source%\%File%" "%%A\%Sub%">Nul

Xcopy directory pattern matching

I need to copy source file to destination folder using bat file.
I have created this:
#echo off
set source="D:\Folder1\file.dll"
set destination="D:\Test\TestCopy*\Test\"
xcopy /s /y %source% %destination%
pause
The destination path I have is TestCopy.2.5.3.6. This numbers can change. So specifying TestCopy* is not working. Specifying TestCopy.*.*.*.* also not working.
How can I solve this?
That is not the way xcopy is used; it cannot copy directory structures/files to multiple folders, e.g. copy file random.ext to C:\folder1\test, C:\folder2\test, etc.
Also, no need to use xcopy to copy files. Just use copy instead.
To achieve this, use:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do (
copy %source% "%%~fA\Test\"
)
popd
Or, better one-line for /F:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do copy %source% "%%~fA\Test\"
popd
This is for the situation when you have multiple TestCopy* subfolders:
::It's better to comment out #echo off when you are testing the batch file
::#echo off
::Moved the open quote's position, so the source variable won't have quotes inside.
set "source=D:\Folder1\file.dll"
for /F "delims=" %%i IN ('dir /ad /b "D:\Test\TestCopy*"') do call :fcopy "%%i"
::Comment or remove pause when it's okay.
pause
goto :eof
:fcopy
if not exist "D:\Test\%1\Test\" goto :eof
xcopy "%source%" "D:\Test\%1\Test\"
Used a sub procedure :fcopy, pass each TestCopy* subfolder name to it.
All :: starting lines are comment lines. They are not executed.
I think it's a good habit to quote every path variable when used, but don't include the quotes themselves in the variable -- that's why I moved the quote in the set source line.

Copy specific file from source directory to target directory and sub-directories using batch script

I want to copy a specific file to a target directory structure meaning to all folders and sub-folder and sub-sub-folder. Basically to a directory tree.
I tried using robocopy, but it has only option to defile level on source and not on target
This is what I have tried so far with robocopy and simple batch
:: copies to only one target directory
robocopy "%SOURCE_FILE_DIR_PATH%" "%TARGET_ROOT_FOLDER_PATH%" %FILE_NAME%
:: copies to only 1 sub-level
for /f "delims=" %%a in ('dir /b "%TARGET_ROOT_FOLDER_PATH%"') do (
for /f "delims=" %%b in ('dir /b "%TARGET_ROOT_FOLDER_PATH%\%%a"')
do (
copy /y "%FILE_NAME%" "%TARGET_ROOT_FOLDER_PATH%\%%a\%%b"
)
)
You can combine the /D and /R options to iterate a directory tree. So I think this would work for you.
set "TARGET_ROOT_FOLDER_PATH=C:\folder\subfolder"
set "FILE_NAME=foo.txt"
FOR /D /R "%TARGET_ROOT_FOLDER_PATH%" %%G IN (*) DO COPY /y "%FILE_NAME%" "%%G"
You could modify your code slightly to use the /AD and /S options of the DIR command.
for /f "delims=" %%G in ('dir /ad /b /s "%TARGET_ROOT_FOLDER_PATH%"') do copy "%FILE_NAME%" "%%G"

How to delete specific hidden sub folders recursively with batch

A bad program left several ".data" hidden folders in every sub directory.
I have tried this so far, and it doesn't work...
RD /ah /s /q "D:\This Folder\.data"
I have many folders and sub-folders, each one has a ".data" hidden folder there that I would like to remove. If it helps, I would like to delete all the hidden folders because they are all ".data".
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims= eol=" %%a in ('
dir /adh /s /b 2^>nul
') do if /i "%%~nxa"==".data" echo rd /s /q "%%~fa"
This executes a dir command to retrieve a recursive (/s) list of hidden directories (/ahd) in bare format (/b). This list is processed by a for /f command that will check for each match if it is a .data folder. If it matches the condition, the folder and its contents are removed.
note the rd commands are only echoed to console. If the output is correct, remove the echo command.
#ECHO OFF
for /f "tokens=*" %%F in ('dir /s /b /o:n /adh') do (
if "%%~nxF"==".data" rd /s /q "%%F"
)
dir /s /b /o:n /adh gives you all folders and subfolders skipping files. for /f iterates over all these folders. %%~nxF extracts the last folder name from the whole path so we can check whether it is .data. If this is the case rd /s /q %%F deletes the folder.
Read what you get when you type for /? then:
Use the /d
FOR /D %variable IN (set) DO command [command-parameters]
If set contains wildcards, then specifies to match against directory
names instead of file names.
and the /r switches together
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.
To scan folders recursivly
for /d /r . %%a in (data*) do rd /s /q "%%a"

copy directories only cmd

I need to copy an unknown number of directories (including the files inside them) from one location to another with a batch file.
My only problem is that I must not copy the files that are located in the same location as the directories.
For example:
Let say c:\Folder\ contains the directories: Dir1 and Dir2 and the file: f1.
I want to copy c:\Folder\Dir1 and c:\Folder\Dir2 (and the files inside them) to c:\Location directory but not file: f1.
Help please!
Try this:
#echo off
setlocal enabledelayedexpansion
REM Set variable
set _SOURCE="C:\Temp\Test\"
set _DESTINATION="C:\Temp\New\"
REM Change Direction
pushd %_SOURCE%
FOR /D %%a in (*) DO xcopy /S /I %%a %_DESTINATION%%%a
/D : Just directories
/S : Copy subdirectories
/I : Targets are directory (not file)
How about something like...
FOR /F "usebackq tokens=*" %%d IN (`DIR /AD /B C:\FOLDER`) DO (
IF NOT EXIST "C:\LOCATION\%%d" (MKDIR "C:\LOCATION\%%d")
XCOPY /E "%%d" "C:\LOCATION\%%d"
)

Resources