Using ROBOCOPY or Batch Script to Copy .doc File From Different Sub-Directories - command-line

I currently have this folder structure:
C:\Quarter1\Folder100\Q1Review100.doc
C:\Quarter1\Folder101\Q1Review101.doc
...
C:\Quarter1\Folder120\Q1Review120.doc
I also have another directory following the same structure except without the .doc files:
C:\Quarter2\Folder100\
C:\Quarter2\Folder101\
…
C:\Quarter2\Folder120\
My question is, how can I write a batch script or use ROBOCOPY so that I can copy all the .doc files from:
C:\Quarter1\Folder100\*.doc
C:\Quarter1\Folder101\*.doc
…
C:\Quarter1\Folder120\*.doc
to a directories:
C:\Quarter2\Folder100\
C:\Quarter2\Folder101\
…
C:\Quarter2\Folder120\
But instead of Q1Review100.doc as the name, I’d like to rename Q1 to Q2, so the file should be copied and renamed to Q2Review100.doc.
Please let me know if I need to clarify this more.

You can issue two commands:
copy all files:
robocopy C:\Quarter1 C:\Quarter2 /S
Replace Q1 in all filenames to Q2:
for /f "tokens=* delims= " %i in ('dir /b /s "c:\Quarter2\*.doc"') do Set LIST=%i& set LIST | ren "%~fi" "%LIST:Q1=Q2%"
Note: if you write it in a batch file replace %i with %%i

Related

Renaming all files in another folder?

I found this code from a post from 2 years ago.
It works great, but is it possible to set a destination for another folder?
This BAT file renames all .jpg files in the current folder. Im wondering if it is possible to input a destination to rename all jpgs in another folder without going to the folder itself.
.bat file is located: "C:\Users\%currentuser%\Desktop\Sorted Files\New folder\JPG"
I want it to sort the jpg files located: "C:\Users\%currentuser%\Desktop\Sorted Files\New folder\JPG\New folder"
set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
ren "%%i" "!a!.jpg"
set /a a+=1
)
WOW ok all i had to do is use
cd C:\folder path
I cant believe it was that easy LOL

How to Copy files that are in a directory to another directory recursively in Windows?

I have to create an script to copy files from a folder structure to other.
My source folder structure is similar to this:
-RootFolder
--ParentFolder1
--SubParentFolder1
--ToCopy
/*Here are the files to copy*/
--SubParentFolder2
--ParentFolder2
--OtherSubParentFolder
--ToCopy
/*Here are the files to copy*/
--ParentFolder3
--OtherSubParentFolder2
I want to copy the files that are in the "ToCopy" folders, into another folder, with this structure:
Destination folder structure:
--TargetDirectory
--SubParentFolder1
//Here the files that were in the ToCopy folder inside the SubParentsFolder1
--OtherSubParentFolder
//Here the files that were in the ToCopy folder inside the OtherSubParentFolder
Notice that I use the name of the "ToCopy" parent folder in the destination subfolders.
I know how I would do this with code (like C#), but I am at a lost on how to achieve it with a Batch file. Is it even possible? Or I would need to use something like powershell?
How can I copy my files following the structure I described?
I think, this should work...
$Folder= gci -path "d:\pstest" -recurse -Filter "ToCopy" | where { $_.psiscontainer }
Foreach ($Foldername in $Folder) {
$Destinationfolder=$Foldername.Parent
copy-item $Foldername.fullname -Destination "d:\Outputfolder\$Destinationfolder" -recurse
}
Hi to follow is a script I hacked away (via help from stack overflow), that reads the files from a txt document, then requests destination folder input and also src folder name it then just goes and recursively copies all the files to the new folder without keeping the old subfolder structure.
I will update this in future with the link to the person that I got the base template from for the admin area, but to keep in mind once you click that Batch can run as though it was a php script then everything makes sense. Took me whole day to research every command and alternative on SS64.com
Major thing to note is the pushd "%~dp0" this I use to make sure batch always uses my current directory as root.
As said I will do a proper write up on this and further stream lining since I am using it actively for moving files during a woocommerce shop update. P.S. the text file name should be entered without the .txt extention and every file name should start on a new line. Also if the destination directory does not exist it will create it. Use excel maybe to list the names then for renaming could output to new column and compile the batch rename command copy to new batch run first batch to fetch files and second batch to rename to preferred title, I do it in steps to keep my sanity.
Sorry was just a example of how I use it, but yes go ahead and enjoy hope this works for you.
#echo off
CLS
setlocal EnableDelayedExpansion
REM Changes root path to relative of current bat folder
pushd "%~dp0"
REM finds files in provided .txt file and copies them to destination directory
REM CHECK FOR ADMIN RIGHTS
COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
IF ERRORLEVEL 1 GOTO:NONADMIN
DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
:ADMIN
REM GOT ADMIN RIGHTS
COLOR 1F
ECHO Hi, %USERNAME%!
ECHO Please wait...
set /p DEST_DIR="Copy files to:"%=%
set /p SEARCH_DIR="Copy files from:"%=%
#echo.
#echo Please check folder name for accuracy.
#echo Copy files to: %DEST_DIR%
#echo Copy files from: %SEARCH_DIR%
set /p CORRECT_FOLDERS="Are these correct? (please check spelling) y/n:"
if '%CORRECT_FOLDERS%'=='y' GOTO:YES_ANSWER
if '%CORRECT_FOLDERS%'=='n' GOTO:NO_ANSWER
COLOR 2F
ECHO.
PAUSE
GOTO:EOF
:NONADMIN
REM NO ADMIN RIGHTS
COLOR 4F
ECHO.
ECHO PLEASE RUN AS ADMINISTRATOR
ECHO.
pause
GOTO:EOF
:YES_ANSWER
#echo.
#echo you answered yes
#echo.
if exist %DEST_DIR% GOTO:READ_DATA
if not exist %DEST_DIR% md %DEST_DIR%&GOTO:READ_DATA
PAUSE
:NO_ANSWER
#echo.
#echo you answered no
set /p TRY_AGAIN="Try again? y/n:"
if '%TRY_AGAIN%'=='y' GOTO:YES_ANSWER
if '%TRY_AGAIN%'=='n' GOTO:EXIT_PROGRAM
PAUSE
:EXIT_PROGRAM
#echo.
#echo "So Sorry"
PAUSE
GOTO:EOF
:READ_DATA
#echo.
set /p GET_FILENAMES="What is the name of the text file your filenames are stored in?"%=%
if exist %GET_FILENAMES%.txt #echo We will now read and copy the files for you, have some coffee might take awhile & GOTO:WRITE_DATA
if not exist %GET_FILENAMES%.txt #echo Filename does not match, please type only the name without .txt extention & GOTO:READ_DATA
PAUSE
:WRITE_DATA
#echo.
#echo reading file name...
for /f "usebackq delims=" %%a in ("%GET_FILENAMES%.txt") do (
for /r "%SEARCH_DIR%" %%b in ("%%a*") do (
#echo Copy Started...
copy "%%b" "%DEST_DIR%\%%~nxb"
)
)
#echo Copy finished, please review actions. Lekker Man.
PAUSE``

Batch file for loop to unzip files and get timestamps of zip file contents

I have a daily process which generates some zip files out of files that are being created by other processes. I need to create a daily log file which indicates the timestamps the contents of one specific file of each zip file that is found.
I created the following batch script which seemed to work yesterday on my test system, but not anymore today, no idea why...
set VersionDirectory=C:\Test\VersionX\
set ResultOutputFile=C:\Test\LogFile.txt
for /f %%f in ('dir /b %VersionDirectory%\Installable\Packages\pattern*.zip') do (
mkdir %temp%\%%f\
unzip -extract -dir %VersionDirectory%\Installable\Packages\%%f %temp%\%%f\ > nul
for %%a in (%temp%\%%f\InstallScript.xml) do set InstallScriptXMLDate=%%~ta
rmdir /s /q %temp%\%%f\
echo %%f [package from %InstallScriptXMLDate%] >> %ResultOutputFile%
)
Short summary of what this file is supposed to do:
Loop through each pattern*.zip file in C:\Test\VersionX\ directory
unzip this file to the %temp%\%%f directory (where %%f is the filename)
Get the timestamp of the %temp%\%%f\InstallScript.xml and put it in the %InstallScriptXMLDate% variable
Delete the %temp%\%%f directory
Echo the filename (%%f) and timestamp (%InstallScriptXMLDate%) into the log file
As of now the log file just contains the filenames, followed by the string '[package from ]' string, but missing the actual date timestamp
The unzipping and removing of the zip files is working flawlessly, it's just the timestamp that's not being set.
You are setting a variable and using it in the same block. This cannot work in cmd because environment variables are expanded when a statement is parsed not when it's executed. So when the loop is run all variables have already been replaced with the values they had before the loop.
Put
setlocal enabledelayedexpansion
at the start of your batch and use !InstallScriptXmlDate! instead of %InstallScriptXmlDate%.
Another note: for is perfectly capable of iterating over files by itself, you almost never need to iterate over dir /b output with for /f. In fact, it can introduce problems that can be avoided with
for %%f in (%VersionDirectory%\Installable\Packages\pattern*.zip)

MSDOS command(s) to copy files matching pattern in directory structure to local directory

I have a job that periodically runs and archives files into a folder structure that looks like this:
ArchiveFolder
TimestampFolder
JobnameFolder
job<timestamp>.xml
For a given job, I'd like to collect all xml files in the archive folder into a flat directory (no subdirectories, just all the files) without having to drill down into each one, examine for the proper job, then copy the file.
It seems there should be a fairly straigtforward way of doing this. Any suggestions?
EDIT: I guess I wasn't clear here. The TimeStampFolder will have a name of something like 2011-07-24, the JobnameFolder will have a name like FooFeed or BarFeed, and the job file will have a name like job2011-07-24.xml. There are hundreds to thousands of TimeStampFolders, and each one may have one or more job folders in it. Given a specific job name, I want to collect all the files in all the directories that match that job type, and dump them into the local folder, with no subdirectories.
EDIT1:
SET JOB=JobName
SET OF=OutputFolder
START /wait NET USE Z: "\\ServerName\Sharename\ArchiveFolder" password password_here /USER:domainname\username /P:NO
PUSHD Z:\
FOR /F "USEBACKQ tokens=*" %%A IN (`DIR /b /a:d /s ^| FIND /I "%JOB%"`) DO (
FOR /R %%F IN (%%A) DO (
COPY /Y "%%~fF" "%OF%"
)
)
POPD
It basically locates each subdirectory of ArchiveFolder that includes the JobName in it, then digs into each one that it finds to copy the files out of them.
EDIT2:
Added NET USE to access your network share to perform tasks on the files. If your local machine already has the UNC assigned to a driveletter, you can remove the NET USE command line and change Z: to the assigned driveletter.
#ECHO OFF
FOR /R %%v IN (job*.xml) DO COPY "%%v" c:\out\

Replacing files in a folder with another file with a dos-batch

Is there any example on DOS Batch - Find and Replace command for files?
I need to find all files with some exact name in a folder (and its sub folders) and replace tham all (one by one for ex) with another known file
Would something like "rename" do what you want?
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
Dew
for /f "tokens=*" %a in ('dir c:\myfolder\myoriginalfilename.txt /s /b') do ((del "%a") & copy MyNewFilename.txt "%~dpa")