I'm trying to create a batch file that will do the following:
I have multiple directories and in each one of those directories there is a folder called '06-2015'. I want to create a batch script that will go thru all of those directories and copy the folder '06-2015' and its files and nothing else.
Example:
C:\Files\Accounts\06-2015
C:\Files\Sales\06-2015
C:\Files\IT\06-2015
Is there a way I can create a script that will go something like:
xcopy C:\Files\*\06-2015 C:\Backup\*\06-2015 /s
Or is there a different/better way to do this?
A wildcard characters allowed only in the last path item.
#ECHO OFF
SET "target=06-2015"
IF NOT EXIST c:\backup\%target% MKDIR c:\backup\%target%
FOR /F "delims=" %%G IN ('DIR /B /AD "c:\files"') DO (
if exist "c:\files\%%G\%target%\" (
:: create backup directory if necessary
MKDIR "c:\backup\%%G\%target%\" 2>NUL
XCOPY /S /E /Y "c:\files\%%G\%target%\" "c:\backup\%%G\%target%\"
)
)
Test this: remove the /s and /e if you don't want any folders inside 06-2015 copied.
#echo off
for /d /r "c:\files" %%a in (06-2015?) do (
if /i "%%~nxa"=="06-2015" xcopy "%%a" "C:\Backup\%%~pnxa\" /s/h/e/k/f/c
)
Related
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.
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"
)
I have a folder with lots of subfolders and I want to create a folder in each of these subfolders. As There is a lot of these folders I was wondering if there was a way of automatically doing this with a bat file.
This should work for your problem
FOR /d %A IN (d:\softwares\files*) DO mkdir %A\source
Run this in the folder in question: it works on all subfolders in the tree.
If you only want to process the immediate set of folders then remove the /s
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do md "%%a\folder" 2>nul
echo done
pause
I have a text file containing a list of files and folders. What I want to do is use xcopy to replicate what is written in the text file. My text file looks like this:
"C:\FOLDER"
"C:\FOLDER\FILE1.TXT"
"C:\FOLDER\FILE2.TXT"
"C:\FOLDER\FOLDER2"
"C:\FOLDER\FOLDER2\FILE3.TXT"
For a given output directory "C:\OUTPUT" I would like to replicate the entire structure, so:
"C:\OUTPUT\FOLDER"
"C:\OUTPUT\FOLDER\FILE1.TXT"
"C:\OUTPUT\FOLDER\FILE2.TXT"
"C:\OUTPUT\FOLDER\FOLDER2"
"C:\OUTPUT\FOLDER\FOLDER2\FILE3.TXT"
How can I accomplish this? So far I have written a for loop that reads in each line of the file, but it copies all files if the line is a folder. What I want to do is only copy and create the files and folders that are mentioned in the text file.
#echo off
for /f "delims=] tokens=1*" %%a in (textfile.txt) do (
XCOPY /S /E %%a "C:\OUTPUT"
)
Am I on the right track?
Thank you and best regards,
Andrew
Yes, you are close. Just need to use the existing path as the appended destination path.
Update
#echo off
for /f "delims=" %%A in (textfile.txt) do if exist "%%~fA\*" (
md "C:\Output\%%~pA"
copy /y "%%~fA" "C:\Output\%%~pnxA"
)
Original
If %%A = "C:\Folder\Folder2\File3.txt", then %%~pA = Folder\Folder2
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" echo f | xcopy "%%~fA" "C:\Output\%%~pnxA" /y
)
The if not exist "%%~fA\*" makes sure to only copy the entry if it is not a directory. See Reference for more Techniques and Comments
Type in for /? at the command line to view a list of the variable modifiers. %%~A will remove the surrounding quotations (if any) from the variable.
Post about xcopy prompting issue. and fix #2.
Alternate Setup, since you most likely will not need the xcopy abilities.
#echo off
for /f "delims=" %%A in (textfile.txt) do (
md "C:\Output\%%~pA"
if not exist "%%~fA\*" copy /y "%%~fA" "C:\Output\%%~pnxA"
)
I'm trying find a way to delete a specific folder name and delete all of its contents in Windows. So my D: drive has a 100 folders, inside each of these folders are sub folders called folder1, folder2, folder3, etc. I want to be able to run a command at the root of D: that will search through each 100 folders and delete say folder3, and folder9 and all of its contents.
RD /s /q "folder1"
The above command doesnt like to search through subdirectories.
Anyway of doing with with CMD or do I need a .vbs script or something?
Thanks!
You can use the FOR command to go through the list of subfolders from a given location and run commands on each subfolder. e.g.:
for /F "delims=\" %%I in ('dir /ad /b <someFolder>') DO (
cd "<someFolder>\%%I"
rd /S /Q "folder1"
rd /S /Q "folder3"
)
It would get the list of folders in someFolder, and delete the directories named "folder1" and "folder3" from each subfolder.
Based on the OPs comments, I believe the following will do the trick:
#echo off
for /f "delims=" %%F in (
'dir /b /s /ad "d:\My files"^|findstr /ie /c:"\folder1" /c:"\folder9"'
) do if exist "%%F" rd /s /q "%%F"