I currently use a batch code which transfers files from one place to another. I wish to elaborate on that code to only transfer files which have a certain phrase in their file name.
I did not write the code, and I do not fully understand it but I know what is does.
REM choose desired drive
cd \
Z:
REM change to required directory
cd out
REM cpy all files using * to the desired directory
copy *.dat \\server\f\rug_data\received_transfer
REM delete all files in the folder
REM del *.usr
All I want to do now is add a bit which says only transfer files with 'D0036' in their name.
I have spent time googling but could not find exactly what I was after.
Any help greatly appreciated.
change
copy *.dat \\server\f\rug_data\received_transfer
to
copy *D0036* \\server\f\rug_data\received_transfer
OR, if you mean "All .dat files with D0036 in their name"
copy *D0036*.dat \\server\f\rug_data\received_transfer
* means "match any number of any characters"
Like this?
copy *D0036*.dat \\server\f\rug_data\received_transfer
This works like a charm!
move /-y "C:\(Folder that the contains files)*(Specify a certain character)*" "C:\(Dest Folder)"
This will move everything in the folder with the name that you wrote.
Hope it works!
Logan
Related
How can I copy file to multiple folders using batch files.
I success to copy one file to one folder.
Regards,
Ramzy
Like this? Seriously, just add a line for each folder.
copy C:\file.txt C:\dir1\
copy C:\file.txt C:\dir2\
copy C:\file.txt C:\dir3\
copy C:\file.txt C:\dir4\
Yes, this is an old thread, but still, i spotted some code that definitely needs an improvement!
Here is a much better movement code!
copy C:\Folder file is in (Here you can specify certain extension/keyword) C:\destination\
move /-y "SOURCE FOLDER AND DRIVE(specific keyword/extension)*" "DEST FOLDER AND DRIVE"
That way you dont have to write endless lines of code!
-Logan
Hi, I have many zip files located at g:\toto. These zips contain some files. I would like to extract all zip in a same directory (g:\toto\extracted) then rename various files of the zip.
Example 1 :
www_12567.vp.zip : 3 files : alpha.doc, beta.xls, teta.doc
I would like after extraction, files are renamed with the name of the zip
www_12567.vp.alpha.doc, www_12567.vp.beta.xls, www_12567.vp.teta.doc
Example 2 :
www_12.vp.zip : 3 files : al.doc, bea.xls, tta.doc
www_12.vp.al.doc, www_12.vp.bea.xls, www_12.vp.tta.doc
I found this question, but it talks about .txt and the zip contain one file, so, it doesn't work.
Without knowing the contents of the archive you can't know which files to rename, because you are putting them into a directory that may already contain other files.
This, however, would be much easier if there was a dedicated directory to put the files temporarily. Here's how you could use it:
#ECHO OFF
SET "srcdir=G:\toto"
SET "tgtdir=G:\toto\extracted"
SET "tmpdir=G:\toto\extracted-tmp"
FOR %%Z IN ("%srcdir%\*.zip") DO (
unpack "%%Z" with your favourite tool into "%tmpdir%"
FOR %%I IN ("%tmpdir%\*") DO MOVE "%%I" "%tgtdir%\%%~nZ.%%~nxI"
)
Of course, the temporary directory would need to be empty before running the batch file. You could add DEL "%tmpdir%\*" somewhere before the loop to make sure it is.
One other note is, the above assumes that the archives do not contain subdirectories or, at least, that the files are extracted without subdirectories.
UPDATE
If you are using the 7-Zip archiver to work with .zip files, then this is how your extract command might look:
7z e "%%Z" -o"%tmpdir%"
Disclaimer: I'm not an active user of 7-Zip. This is what I used as a reference to come up with the above command:
7-Zip Command-Line Examples
I am completely new to this, but I am trying to create a .bat file that will allow me to rename a pair of files within a designated folder and move them into a subfolder. The part I am having trouble with is that I am wanting a prompt to come up to identify/select the files to be renamed and moved.
Example file names are:
A1234, A1235, A1236, B1234, B1235, B1236, etc.
Is there a way to bring up a prompt that allows the user to type the shared name (ex 1234)of the files and rename and move both files to the designated subfolder?
Any and all help would be appreciated!
Suggested approach
for part of problem
part I am having trouble with is that I am wanting a prompt to come
up to identify/select the files to be renamed and moved. Is there a
way to bring up a prompt that allows the user to type the shared name
(ex 1234)of the files and rename and move both files to the designated
subfolder?
Do a search operation using wildcard, like "?1234" for the case highlighted above ( should be made generalized for all acceptable and expected patterns "*1234*" is the generic most )
Now do a RENAME inside a For loop on the results obtained by search.
As you suggest you are a newbie with Batch, following tutorials will help you build your file. Look for elements like Variables, For Loop
Batch Tutorial
Here you go
#echo off
set /p file=Please type shared name:
for %%a in (C:\Folder\?%file%.*) do (
move "%%a" subdir
ren "subdir\%%a" newname.*
)
I'm have a lot of pictures in a file system. I'm trying to figure out a way to rename all of the files, and if possible put them in one folder. At the very least rename them where they are.
They are currently in a hierarchy like this:
folder1\folder2\filename.jpg
Is there any way I can write a script to rename the files so the name of the actual file would be:
folder1-folder2-filename.jpg
I'm not sure how to go about this. Any suggestions or a push in the right direction would be greatly appreciated.
try this..
for %%* in (.) do set name=%%~pn*
set name=%name:\=-%
set name=%name:~1%
%name% now has the folder name. add this to your file name.
I need help making a batch file in ms dos to do certain commands like:
Request from you to first press any key
List the contents of the C:\WINDOWS directory
Create a subdirectory on one of your local drives. Use your initials to name the subdirectory.
Copy all the text files from C:\WINDOWS directory into the new subdirectory.
Print one of the text files copied to your new subdirectory.
for 5. I think I need to make a call to a command to print
You need to create a .bat file that has the following text:
a)Request from you to first press any key
pause
b) List the contents of the C:\WINDOWS directory
dir c:\windows
c) Create a subdirectory on one of your local drives. Use your initials to name the subdirectory.
md c:\ro
d) Copy all the text files from C:\WINDOWS directory into the new subdirectory.
copy c:\windows\*.txt c:\ro
Regarding printing files I'm not sure, if I remember correctly the simplest way is something like:
type file.txt > LPT1
To create a .bat file, you should type in:
copy con filename.bat
... print the relevant lines above
When finished press Ctrl-Z and Enter.
So, all in all, you should type in:
copy con filename.bat
pause
dir c:\windows
md c:\ro
copy c:\windows\*.txt c:\ro
type file.txt > LPT1
And then Ctrl-Z and Enter.
Update: If you don't want the commands to be printed to the user, you can add another line before pause that will contain #echo off.
Check out Rob van der Woude's Scripting Pages, specifically the Batch Files section. He has an excellent reference with lots of tips and tricks that will help with most of these tasks.
Command line help is your friend here for most of this. You will need to have input parameters specified for a number of the things you;re asking for, though.
As a quick search, try this article.