I have a file 00185.txt, how do I create a batch file to duplicate this file and rename to 00186.txt, then continue to duplicate another 150 times by an increment of one.
eg. original file is 00185.txt, duplicate this and rename to 00186.txt, then the next would be 00187.txt, all the way to 00300.txt.
Any help would be greatly appreciated. Thank you.
#echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
if not exist "00185.txt" echo Stack overflow example > "00185.txt"
set first=186
set /A last=%first% + 150
for /L %%A IN (%first%,1,%last%) do #copy /B /Y "00185.txt" "00%%A.txt" > nul
Related
Help me sir i try more than 100 times but it not exactly works for me.
Currently getting results in wrong way:-
Given below batch script create folder from this file (7101-1- kutana.pdf) name with .pdf extension but not exactly what i need.
Results i need in right way a/q to given below steps
For example
Step 1:-I want to remove 1st five character from file name (7101-1- kutana) and also i want to remove extension from folder name.
Sourcedir=e:\automovesystem\temp
A. source pdf file name :- 7101-1- kutana.pdf
Step 2:-
Create directory under destdir name of 1-(space)kutana and award
B. destdir=e:\automovesystem\prpl
Created directory e:\automovesystem\prpl\1- kutana\award
Step3.check if already folder name available according to step 1 file name.
Step 4: - move same file in same folder which is created in step 2 under sub directory.
#echo off setlocal enableextensions disabledelayedexpansion
Set "sourcedir=e:\automovesystem\temp"
Set "destdir=e:\automovesystem\prpl"
For /f "eol=| tokens=1* delims=-" %%a in ('dir /b /a-d-h "%sourcedir%\*-*" 2^>nul') do (
Md "%destdir%\%%b\award" 2>nul
Move /y "%sourcedir%\%%a-%%b" "%destdir%\%%b\award" )
Endlocal
Additional to your own answer, I probably would have done it a litte more like this:
#Echo Off
SetLocal EnableExtensions
Set "SRC=E:\automovesystem\TEMP"
Set "DST=E:\automovesystem\PRPL"
If Not Exist "%SRC%\" (Exit /B) Else If Not Exist "%DST%\" Exit /B
For /F "Delims=" %%G In ('%SystemRoot%\System32\where.exe "%SRC%":"????-?*-?*.*"
2^>NUL') Do For /F "Tokens=1,* Delims=-" %%H In ("%%~nG"
) Do %SystemRoot%\System32\Robocopy.exe "%%~dpG." "%DST%\%%I\NOTICE" "%%~nxG"^
/Mov 1>NUL 2>&1
All help with this goes to #XionicFire he reworked the whole of this code I'm just posting it for others to use/benefit:
This code comes from a mix of several different people #PrahladDixit & #Compo, the code was not working we couldn't tell why after a LOT of search seems it suffered the usual... "a pro wrote this so no one understands what he did" syndrome, where us worms cannot figure it out, we remade it and organized it so normal human "worms" can understand what its doing and made it a little friendlier so its easier to mod and change as needed, we hope this helps others, works great in windows 11
Code
#ECHO OFF
ECHO This file will create Individual folders inside the current folder using the following Parameters and sort all *.JPG files in current directory accordingly
REM To change target or source directory simply type it in the variable field
SET "SRC=%~dp0"
SET "SRC=%SRC:~0,-1%"
SET "DST=%~dp0"
SET "DST=%DST:~0,-1%"
ECHO.
REM For Diagnostics & Troubleshooting Purposes
ECHO Source: %SRC%
ECHO Destination: %DST%
ECHO Characters: 19
ECHO Where: %SystemRoot%\System32\where.exe "%SRC%":*.jpg
ECHO.
ECHO To Cancel this operation press CTRL-C
PAUSE
SetLocal EnableDelayedExpansion
If Not Exist "%SRC%\" (Exit/B) Else If Not Exist "%DST%\" Exit/B
For /F "Delims=" %%A In ('%SystemRoot%\System32\where.exe "%SRC%":*.jpg') Do (
SET "FNX=%%~nA"
REM Replace 19 for the number of characters from the start of the filename you wish to use as Base for folder creation
SET "FNX=!FNX:~,19!
REM For Diagnostics & Troubleshooting Purposes
REM ECHO !DST!\!FNX!\
If Not Exist "!DST!\!FNX!\" (MD "!DST!\!FNX!" 2>NUL
If ErrorLevel 1 ECHO Unable to create directory !DST!\!FNX!)
If Exist "!DST!\!FNX!\" (MOVE /Y "%%A" "!DST!\!FNX!">NUL 2>&1
If ErrorLevel 1 ECHO Unable to move %%A to !DST!\!FNX!)
)
ECHO ************************ DONE ************************
PAUSE
REM Use in the case of running multiple scripts in a row
REM EXIT/B
I had less knowledge of batch scripting that's why not sleep from yesterday continue
working on to make AutoMoveFilesystem but also i am great thankful to provide stack overflow platform to create this code. i can't able to do without this plateform.
#Echo Off
Set "SRC=E:\automovesystem\TEMP"
Set "DST=E:\automovesystem\PRPL"
SetLocal EnableDelayedExpansion
If Not Exist "%SRC%\" (Exit/B) Else If Not Exist "%DST%\" Exit/B
For /F "Delims=" %%A In ('Where "%SRC%":?????????*.*') Do (Set "FNX=%%~nA"
If Not Exist "%DST%\!FNX:~5,50!\NOTICE" (MD "%DST%\!FNX:~5,50!\NOTICE" 2>Nul
If ErrorLevel 1 Echo Unable to create directory %DST%\!FNX:~5,50!\NOTICE)
If Exist "%DST%\!FNX:~5,50!\NOTICE" (Move /Y "%%A" "%DST%\!FNX:~5,50!\NOTICE">Nul 2>&1
If ErrorLevel 1 Echo Unable to move %%A to %DST%\!FNX:~5,50!\NOTICE))
Timeout -1
Exit/B
Based on several articles on the internet I have written a batch script that:
scans defined location looking for .png files,
creates path to the new location for each file (..\first 4 characters of filename\first 9 characters of filename)
moves files to the new location
The script works fine, however I would like it to stop displaying errors and commands in command line window and display "PROCESSING..." instead? I have tried echo off, >nul and 2>nul with no success.
Here's the script:
#echo off
setlocal enabledelayedexpansion
cls
pushd E:\Archiwum\
for /f "tokens=*" %%1 in ('dir /a-d /b E:\Archiwum\*.png') do (
set filename=%%1&set dirname=!filename:~0,4!\!filename:~0,9!
if not exist E:\Archiwum\!dirname! (md E:\Archiwum\!dirname!)
move %%1 E:\Archiwum\!dirname!\
)
Here's a quick rewrite of your script
#Echo Off
If Exist "E:\Archiwum\*.png" (CD /D "E:\Archiwum") Else Exit /B
SetLocal EnableDelayedExpansion
ClS
For %%A In (*.png) Do (Echo Processing %%A
Set "filename=%%~nA"
Set "dirname=!filename:~,4!\!filename:~,9!"
If Not Exist "!dirname!\" MD "!dirname!"
If Exist "!dirname!\%%A" (Echo Name conflict %%A already exists.
) Else Move "%%A" "!dirname!" > Nul)
You need to be careful of spaces in your file names in order to prevent having directory names with trailing spaces as well as the other things I mentioned in my comments.
Thanks for your feedback. I made two modifications to your solution and now it works just fine.
#Echo Off
If Exist "C:\Archiwum\*.png" (CD /D "C:\Archiwum") Else Exit /B
SetLocal EnableDelayedExpansion
ClS
#Echo Processing...
#Echo Off
For %%A In (*.png) Do (
Set "filename=%%~nA"
Set "dirname=!filename:~,4!\!filename:~,9!"
If Not Exist "!dirname!\" MD "!dirname!"
If Exist "!dirname!\%%A" (Echo Name conflict %%A already exists.
) Else Move "%%A" "!dirname!" > Nul)
I added second #Echo Off - without it commands from for loop where still shown in command window.
I moved #Echo Processing as I wanted it to be shown only once with all of the operations done in the background.
Thank you a lot for your help.
I have this code:
#echo off
for /f "delims=" %%a in ("%~dp0\files\text\lead_r.txt") do set /p key= < lead_r.txt
ECHO %key%
PAUSE
What I want it to do is to open a specific text file, read it and then write what's in it to %key%. It works when lead_r.txt and the batch file are in the same folder but it fails when I move lead.txt to \files\text. There are no files with spaces in \files\text so i don't know where the problem is.
Thanks
Hard to understand your goal, but try next code snippet:
#echo off >Nul
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ("%~dp0\files\text\lead_r.txt") do (
rem the same file in next line
rem set /p key=< lead_r.txt
set /p key= < %%a
ECHO !key!
PAUSE
)
endlocal
Here mind EnableDelayedExpansion keyword of setlocal command = Expand variables at execution time rather than at parse time, if used !key! syntax instead of %key% one.
I'm wanting a batch file that I can drag a text file onto (preferrably multiple text files at once) which will read each text file line by line and add each line to a specified destination text file. Destination text file will not contain any duplicated lines, and will be sorted alphabetically. A source file will never contain the same line twice, but may contain non-alphanumeric characters such as: { - : _ ~ !
Example:
a.txt:
apple
banana
garbage carrot
{Elmer Fudd}
b.txt
1 tequila
2 tequila
3 tequila
garbage carrot
{Bugs Bunny}
destination.txt before:
{daffy duck}
floor
destination.txt after dragging a.txt and b.txt onto the batch file:
{Bugs Bunny}
{daffy duck}
{Elmer Fudd}
1 tequila
2 tequila
3 tequila
apple
banana
floor
garbage carrot
I've got it started:
#echo off
setlocal disabledelayedexpansion
set "sourcefile=%~1"
echo "%sourcefile%" > temp.txt
for /f "delims=;" %%F in (%sourcefile%) do (
echo %%F>>temp.txt
)
del /q destination.txt
ren temp.txt destination.txt
It copies the file dragged into a temp file, but I can't figure out how to get it sorted. The sort command does NOT work for me, it just hangs the program up. All help is appreciated. Thank you!
#echo off
setlocal enabledelayedexpansion
set "prev="
echo "%~1"
copy /y destination.txt+"%~1" temp.txt >nul
(
for /f "delims=" %%F in ('sort temp.txt') do (
if "!prev!" neq "%%F" echo(%%F
set "prev=%%F"
)
)>destination.txt
del temp.txt
should work for you (I've not tried it)
No need to process the new file with for /f - copy a+b c concatenates a and b into c. The /y forces overwrite of the destination if it already exists.
Then process each line, echoing the line read from the sort if this line does not match the previous - but "enclosing the strings in quotes" so that batch knows to process the string as a single unit if it contains spaces or other separators.
The (...echo ...)>file format (re)creates file with the echoed data rather than sending the data to the screen.
If I understand you right then this should suffice:
#echo off
:loop
type "%~1" >>"c:\destination.txt"
shift
if not "%~1"=="" goto :loop
sort < "c:\destination.txt" > "%temp%\random file.txt"
move "%temp%\random file.txt" "c:\destination.txt"
Save as batch file. This is an hybrid batch/jscript file. The file processing/sorting is done in the batch part. Then, the javascript part is invoked to eliminate the duplicated lines.
EDITED - To adapt to comments
#if (#This==#IsBatch) #then
#echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
rem If there are no files, nothing to do
if "%~1"=="" goto endProcess
rem Configure the output final file
set "outputFile=destination.txt"
if not exist "%outputFile%" >"%outputFile%" break
rem Configure and initialize temporary file
set "tempFile=%temp%\%~nx0.%random%%random%.tmp"
find /v "" <"%outputFile%" >"%tempFile%"
rem Iterate over the file list sending output to temporary file and deleting input file
for %%a in (%*) do (
find /v "" <"%%a" >>"%tempFile%"
rem del /q "%%a" 2>nul
)
rem Process temporary file into outpufile, sorting and eliminating duplicates
type "%tempFile%" | sort | cscript //nologo //e:Javascript "%~f0" > "%outputFile%"
rem Cleanup
del /q "%tempFile%" 2>nul
:endProcess
endlocal
exit /b
#end
// **** Javascript zone *****************************************************
var stdin=WScript.StdIn, stdout=WScript.StdOut, previous=null, current;
while (!stdin.AtEndOfStream){
if (previous !== (current=stdin.ReadLine())) stdout.WriteLine(current);
previous = current;
};
I have hundreds image files. I want to do 2 tasks with batch script.
1) I want to rename files with the name without '_' if any file have and move them to temp folder.
2) if any file duplicates with file name then take it any of the file and move it to specified temp folder.
Anyone knows how to do this? Thanks in advance...
for the renaming part...you could try this approch...
#echo on
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%i in ('dir *.txt /b/a-d') do (
set name=%%i
set name=!name:_=!
ren %%i !name!
)
For the file moving part...could you please elaborate the requirement a little more?
--Nihar