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

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")

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

DOS: Copy one file (oldest) at a time

There is a folder which contains many zip files. I want to copy only oldest zip file on each commmand execution. Please help? Thanks
From How do I write a Windows batch script to copy the newest file from a directory? - but reversing the file order
FOR /F "delims=" %%I IN ('DIR . /B /O:D') DO COPY %%I NewDirectoryPath & EXIT

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

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

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\

Get the latest file from a list of files by name and merge it with another file in a Windows batch file?

I have a list of files in a folder. They are named like this: 2011-04-14_00-00-24
Example list:
2011-04-14_00-00-24
2011-04-13_00-01-12
2011-07-08_00-00-28
2010-03-12_00-00-45
...
Now I want to get the latest file according to the filename from that list, in this case its 2011-04-14_00-00-24. The file I get should be merged with another file. How would I get the latest file and do the merge for the 2 files?
Thanks :-)
You can get the latest file with this:
for /f "delims=" %%x in ('dir /o-n /b') do (set "Latest=%%x" & goto le)
:le
I'm not quite sure what you mean with merging; if that is concatenating two files:
copy somefile+"%Latest%" newfile