I want to make a .bat to copy & rename a file multiple times. I want to have a list of names, and an original file, then I want to copy that file and rename it for each name on the list.
How I can do this using a .bat file?
Also is it possible to run winrar fromthe .bat to .rar or .zip every file after copying/renaming?
Example:
$file = "file.tmpl";
$names = "name1, name2, name3, nameetc";
foreach( $names as $name) {
copy $file; //to avoid deleting the original
rename $file to $name;
zip $name; //I dont really need this but if its easy to do i will like to use it
}
So I start with a file.tmpl and I end up with 4 more files (which are a duplicate of file.tmpl) called name1, name2, name3, nameetc.
The example is not a real coding lang, I used a sort of php sintax because is the language I know more.
Do the file names need to be in a string list?
If you can name them in a separate file, like so,
name-one.pdf
name-two.pdf
name-three.pdf
then this batch file will work
SET source_file=%1
SET name_list_file=%2
FOR /F "usebackq delims=," %%G IN (`TYPE %name_list_file%`) DO (
COPY %source_file% %%G
)
You would call it like this
batch-file-name source-file-name name-list-file
In other words, I called the batch file make-copies.bat, and the name file filenames.txt, and I used it to copy a file called mla-play.pdf.
make-copies mla-play.pdf filenames.txt
This also allows you to change the target name list without modifying the batch file.
Hope this helps.
Related
I have a *.bat file in various folders which renames certain files so project number, project name and initials are included in the filename. So far running the file will ask the user to enter project number, project name and initials which are saved as variables (no, project and initial). Each original filename is kept but in brackets will be the required projects specific data). So far it works but what I would like is set up a batch file where the name sets the template for the project specific data e.g.filename of bat file could be:
Rename <XY00.000> (Projectname),XYZ.bat
If in CMD code you could retrieve the filename of the executable bat file (should be only 1 in the folder) and then with delimiters you would splice the filename and store the relevant strings in different variables e.g. what is inside <> will be variable "no", whats inside () will be variable "project" and what is past the , is "initial". If I have those variables the file renaming is as in my current procedure. Please help as I'm not familiar with CMD code or Powershell (which might be alternative option) and it took me long to assemble the existing code. Basically I want to change the input part (see set /p) to something which does:
get filename of bat file
extract 3 text strings from the filename (use of delimiters like ( < ,)
store the 3 text strings as parameters (NO, PROJECT , INITIAL)
the rest of the existing code should then work to rename the files in the folder
Thanks.
Existing code as example below:
cls
echo off
pushd "%~p0" 2> nul
pushd "\\%~p0" 2> nul
echo
attrib -r *.* /s
set /p no= Enter Project Reference in style XY00.000:
set /p project= Enter Project name (without any underscores):
set /p initial= Enter your Initials:
for /f "tokens=1* delims=(" %%i in ('dir /b *.xlsm') do ren "%%i(%%j" "%%i(%no% %project%_%initial%).xlsm"
for /f "tokens=1,2* delims=()" %%i in ('dir /b *.docm') do ren "%%i(%%j)%%k" "%%i(%no% %project%_%initial%)%%k"
I replaced the user input with:
set infovariable=%~n0
which safes the filename of the bat file as a variable and uses it later to rename appropriate files with it. The bat file is named exactly what I want as a text. Job done!
I have a directory which contains multiple file (~1000) with names in the form of ABC.txt, ABC.1.txt, ABC.2.txt, XYZ.txt, XYZ.1.txt and so on.. I want to consider these names based on the first index if we split them by .. For example then names will be read as ABC XYZ and so on.. Then I want to remove those files for which these first index does not exist in an other (reference file). Given the file names mentioned above, let's say my other (reference file) only contains 1 name and that is XYZ. So the files that will be kept in the directory will be XYZ.txt and XYZ.1.txt and everything else which does not have the exact prefix as XYZ will be removed. I said exact because it might happen that there will be a file with name XYZA.txt, so there should be an exact match in order to keep that file.
Can anybody help me with this. Thank you very much.
EDIT: One directory contains all the files: ABC.txt, ABC.1.txt, ABC.2.txt, XYZ.txt, XYZ.1.txt and the reference file is in another directory as file name reference.txt and is a one-column file containing other directory's file (prefix)names as ABC, XYZ, CDE etc..
try
for f in *.*;
do if grep -qF "${f%%.*}" file;
then echo "skip $f";
else echo "rm $f"; fi;
done
searches all files with a dot in file name, extract the prefix until the first dot, compare literally in the file with not to be deleted names, and echo the rm file command. Make sure your file with names doesn't have a dot, otherwise it will be removed as well.
if looks fine remove echo before rm.
I need to extract the filenames as well as path in a csv file from different subdirectories in a parent directory using unix shell script .
Requirement : I have different subfolders in a parent. Every subfolder ha one image file. Now , I need to list the filename and path of that file , And need to write the filenames and path into a csv file for all the sub directories.
Please help me out.
-Sudhir.
If I understand correct, you want write filename and path to csv file.
#!/bin/bash
echo "Dir, Name" > "csvfile.csv"
files=$(find /YOURPATH -type f)
for f in $files
do
dir=$(dirname $f)
name=$(basename $f)
echo "$dir, $name" >> "csvfile.csv"
done
Find all files in /YOURPATH, write path and filename to csvfile.csv.
Or If you want to find just image file and if you know type, you can use like this:
find /YOURPATH -type f -name *.jpg
I have a folder with the files a.txt, b.txt, and c.txt in it. and I want to use a for-loop to get the names of these files, store them in a variable, and then add them to another text file. The text files are located in the same file as my bat file. Is it possible to do this? If so please show me, these for loops confuse the crap out of me...
What i have now
#echo on
SET name=hey
echo >text.txt
for %F in (*.*) do (set name=#fname
echo name >> text.txt)
#Echo OFF
(For %%# in ("*.txt") do (
Set "FileName=%%~n#"
Call Echo %%FILENAME%%
))>"MyFilenames.txt"
Pause&Exit
NOTE 1: Files are stored in "Filename" var, but is not really necessary, you can directly write the filename to the textfile.
NOTE 2:If you want a recursive loop through files use the /R switch of For command.
NOTE 3:If you want also the file extension change this: "%%~n#" to this else: "%%~nx#"
**UPDATE:**
An alternative script:
#Echo OFF
For %%# in (*.txt) do (Echo %%~n#>> "MyFiles.txt")
Pause&Exit
I am creating a batch file to consolidate some hard coded text with a few of other existing text files.
for this I am using the below.
set "txtFile=.\text.txt"
call:Append "C:\test 123\test.txt" %textFile%
over here, when I execute it, it thros an error as it is not able to proceed with the path as it has spaces.
how should this be addressed.
I have no idea what your append batch file is doing, but you can simply use copy to concatenate two files.
It's not clear to me what the needs to be appended to what, but the following will append the contents of text.txt to C:\test 123\test.txt by writing everything to C:\test 123\test.txt.
set txtFile=.\text.txt
copy "C:\test 123\test.txt" /a + %txtFile% /a "C:\test 123\test.txt"
If you want a different output file, just change the last parameter.
Btw: it's better to not rely on a specific working directory
The following:
set txtFile=%~dp0text.txt
makes sure that the text.txt is used that is in the same directory as your batch file.