rename file with progressive enumeration - dos

I have a file called mycat.avi inside a folder VIDEO
so that the path is c:\video\mycat.avi
now I would like rename it so that the new re-name become start from C0700.mxf
IF in folder c:\video is jet present another C0700.mxf -----> the file have to renamed C0701.mxf
if in folder c:\video is jet present another C0701.mxf -----> the file have to renamed C0702.mxf
if in folder c:\video is jet present another C0702.mxf -----> the file have to renamed C0703.mxf
... and so on
Supposing mycat.avi become renamed as C0716.mxf (so that now is present as c:\video\C0716.mxf)
--> after the renaming operation, tha batch have to create a .avs file (located alwais in the c:\video folder) that are called mycat_is_C0716.avs that contains:
FFVideoSource("c:\video\C0716.mxf")
So finally, in c:\video
are present only this files: C0716.mxf and mycat_is_C0716.avs
Thanks

Test this:
#echo off
cd /d "c:\video"
for /L %%a in (0700,1,9999) do (
if exist "mycat.avi" if not exist "C%%a.mxf" (
ren "mycat.avi" "C%%a.mxf"
>"mycat_is_C%%a.avs" echo FFVideoSource^("c:\video\C%%a.mxf"^)
)
)
pause

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

how to copy all files in subfolders to the parent folder

I want to copy all files within subfolders to the parent folder. But want to create a new folder in it by the name "copied" in whichever folder i have copied my batch file. I tried this command.
FOR /R %%# IN (*.*) DO XCOPY "%%#"
It does the job but it keeps asking to rewrite files. Also wasn't sure how to create the folder name in which i have copied my batch file into.

Copy batch file into newly created folder of same name

I want to create a batch file which will search for .docx files in my folder and then for each of them create a separate directory and then copy each .docx file inside that directory. My folder contains of hundreds of files with so long names and housekeeping is a nightmare!
I have searched around and found some code which helps me to create a new directory for each .docx file. Here is the link.. "Win 7: CMD batch file for creating directories based on filenames"
The code which ONLY creates new directory for the file
for %%I in (*.docx) do mkdir "%%~pnI"
However how do I COPY the file INTO the newly created directory with the same name?
E.g file name is 20160611150424.docx (where numbers are YYYY MM DD HH MM SS) and using the code above it creates a file called 20160611150424.
How do i use batch file to copy this file into this folder? Which is the appropriate code to be used?
Many thanks,
NewLearner
Read copy /?: you could copy a file into newly created directory specifying the destination as a folder without file name and extension and copy command will retain them):
for %%I in (*.docx) do (
mkdir "%%~pnI" 2>NUL
copy /B "%%~fI" "%%~pnI\"
)
or specify the destination with file name and extension explicitly as follows:
for %%I in (*.docx) do (
mkdir "%%~pnI" 2>NUL
copy /B "%%~fI" "%%~pnI\%%~nxI"
)
Note that 2>NUL will suppress error message A subdirectory or file a already exists possibly generated by mkdir command if this is the case.
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%%~nxI etc. special page) Command Line arguments (Parameters)
(2>NUL etc. special page) Redirection

How to batch zip an image directory plus one js and one html file, Win 7

I would like to do a batch zip file operation. What each zip file has in common is an images directory and a *.js and a *.html file with the same name.
7zip has a pretty neat command line interface (which is also part of the 7zip documentation; see http://7-zip.org for more information) which can be used within a .bat file to create archives of any complexity.
For a start try the following batch file (e.g. save as generate.bat). It will loop over all subdirectories of the working directory and add all files of the subdirectories within its own .zip file named as the subdirectory. After that the corresponding .js and .html files are added to the archive.
#echo off
SET SEVENZIP=C:\Program Files\7-Zip\7z.exe
:: if 7z.exe is available within your PATH the following is sufficient: SET SEVENZIP=7z.exe
:: remember initial (current) directory
SET INITIAL_DIR=%CD%
:: define working directory
SET WORKING_DIR=%1
IF "%WORKING_DIR%" == "" (
ECHO No input directory given using current directory...
SET WORKING_DIR=%INITIAL_DIR%
)
:: change to working directory
CHDIR /D "%WORKING_DIR%"
:: loop over sub-directories
for /D %%a in (*) do (
ECHO.
ECHO.
ECHO *** Creating %%a.zip ***
:: add all files within images directory with name %%a
"%SEVENZIP%" a "%%a.zip" "%%a\*"
:: add .js and .html file
"%SEVENZIP%" a "%%a.zip" "%%a.js"
"%SEVENZIP%" a "%%a.zip" "%%a.html"
)
:: change back to original directory
CHDIR /D "%INITIAL_DIR%"
:end
ECHO.
ECHO.
PAUSE
To run it either copy it to your working directory or use the Windows Explorer and drag 'n' drop the working directory onto the .bat file.

Merge all files from the current folder and all subfolders in one file using .bat file

I'm looking for a batch file that merges the content of all files in the current directory and files from all subdirectories.
Also, that would be perfect if files were separated by several new lines in the big file and probably contain the filename with filepath above the each file content.
For example, there are two files in the current folder D:\ , where our batch file is located:
1.
d:\file1.txt contains:
some
text here
hahaha
2.
d:\folderabc\file2.mp3 contains:
doremi text
I run merge.bat file on d:\ and it creates a merge file result.txt (or whatever extension) with such content:
=========d:\file1.txt=========
some
text here
hahaha
=========d:\folderabc\file2.mp3=========
doremi
I appreciate if someone share his solution for this problem.
Thank you so much.
Test this with your folder. It assumes the files are text files.
edit: This has a fix - and you can remove the "d:\base\folder" to run it from the current directory.
#echo off
for /r "d:\base\folder" %%a in (*) do (
(
echo =========%%a=========
type "%%a"
echo(
echo(
echo(
)>>"%temp%\temp.file"
)
move "%temp%\temp.file" . >nul
echo done
pause

Resources