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.
Related
I'm looking to archive 10.0000+ files in a similar format:
files:
871-517-06461-1-120-01.pdf
871-517-06461-1-113-01.stp
871-517-06461-1-100-01.pdf
Example dir generated:
871-517-06461-1
NAV
TPD <- (files need to moved here)
INS
files:
871-517-21541-1-100-01.pdf
871-517-21541-1-110-01.pdf
871-517-21541-1-113-01.stp
Example dir generated:
871-517-21541-1
NAV
TPD <- (files need to moved here)
INS
What would be the way to archieve this? I came up with this:
#echo off
setlocal enabledelayedexpansion
for %%A in (*.pdf *.stp) do (
set data=%%A
echo file found !data:~0,16!
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname:~0,16!
if not exist "!folname:~0,16!" (
echo Folder !folname:~0,16! does not exist, creating
md "!folname:~0,16!"
md "!folname:~0,16!\TPD"
md "!folname:~0,16!\NAV"
md "!folname:~0,16!\INS"
) else (
echo Folder!folname:~0,16! already exists >> log.txt
)
echo Moving file %%A to folder !folname:~0,16!
move "%%A" "!folname:~0,16!\TPD"
)
echo Finished
pause
It's working, but how can i simplify this?
Also can i somehow change the character count with setting a variable?
Is it possible to remove the last - sign? So 871-517-06461-1 is named 871-517-064611.
Does this help you out?
It doesn't count characters, (yours should have been 15 anyway), it isolates everything up to the fourth hyphen/dash, -.
#Echo Off
SetLocal EnableExtensions
For /F "Delims=" %%G In (
'(Set PATHEXT^=^) ^& %SystemRoot%\System32\where.exe "%~dp0.":"?*-?*-?*-?*-*.pdf" "%~dp0.":"?*-?*-?*-?*-*.stp" 2^>NUL'
) Do For /F "Tokens=1-4,* Delims=-" %%H In ("%%~nG") Do (
For %%L In (INS NAV TPD) Do If Not Exist "%%~dpG%%H-%%I-%%J-%%K\%%L\" MD "%%~dpG%%H-%%I-%%J-%%K\%%L"
If Exist "%%~dpG%%H-%%I-%%J-%%K\TPD\" Move /Y "%%G" "%%~dpG%%H-%%I-%%J-%%K\TPD" 1>NUL
)
I haven't included all of the pointless echoing of messages, so you'll need to adapt it for those if you really need them. Please however, do not make such changes before you've tested it. The only thing you may need to change is the source path; the above uses %~dp0. for the same location as the running batch file, you could change that to just ., for the current directory, or an actual relative or absolute path, if you prefer, (taking care not to remove their enclosing double-quotes).
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
I have a folder where all my multipass 3d renders are going. I had to start and stop the renders several times which lead to name changes.
names:
Mask_Opening_Scene_Multipass_i1_0230.exr
Mask_Opening_Scene_Multipass_i1_0231.exr
Mask_Opening_Scene_Multipass_i1_0232.exr
The bulk of the files are named:
Mask_Opening_Scene_Multipass0230.exr
So what I need, is to remove the, _i1_
Here is what I have:
#echo off
set list="*_il_*"
echo List of files
echo.
echo.
for /r %%a in (%list%) do (
set file=%%a
echo "!file!" "!File:_i1_=!")
)
pause
Can anyone help me out?
#echo off
setlocal enabledelayedexpansion
set "list=*_il_*"
echo List of files
echo.
echo.
for /f %%a in (%list%) do (
set file=%%a
echo "!file!" "%File:_i1_=%")
pause
or
#echo off
setlocal enabledelayedexpansion
set "list=*_il_*"
echo List of files
echo.
echo.
for /f %%a in (%list%) do (
set file=%%a
echo "!file!" "!File:_i1_=!")
pause
First, you don't have the delayed expansion enabled, so you cant use the variables with "!".
Then, you use /r instead of /f, /r is for paths, /f is for files, so probably is catch a error using /r.
You don't have to especify whats the _i1_ gonna be, you can only put %file:_i1_=%, will make the name disapear, and i think that the delayed var (!) is not necessary for the variables...
I hope i helped you.
Ok. I have this batch file that is designed to remove every file and folder from inside a folder that may or may not be present. The basic structure Follows:
#echo off
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\
echo %CD%
echo %0
echo %~dp0
rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
The reason for the echoes in I'd like to be able to ensure the batch script has actually changed the path to MyDirectory and is deleting the correct files(we had an incident earlier that I'm kindof in hot water for where the script didn't change paths and deleted everything from one of our docs folders).
The echoes return the either the name of the batch file itself, or the path the batch file was run from,instead of "c:\MyDirectory\". So in my case it's from c:\Testing\ (a dummy directory I created to avoid a second oops).
Is there a way to get the currently active directory from inside a batch script, so that I can verify the directory I'm about to empty??
Try this:
#echo off
setlocal EnableDelayedExpansion
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\
echo !CD!
pause
rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
Since you're in an if statement you should use setlocal EnableDelayedExpansion and use ! instead of % for variables.
To nuke the contents of a directory, use this shell script (batch file):
#echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START
:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE
:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd
:DONE
endlocal
i want to make a batch file, This Batch file must look out, into a folder with the name "Draft" and for every sub-folder will be make a search for a .txt file "list.txt" and when finds this .txt file, Then will be execute a copy from the folder "Draft" to the folder "Ready". I have written a small script but i have some issues.
#echo off
:loop
for /d %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do (
SET a=%%i
echo %a%
)
echo Folder is empty or does not exist
timeout /t 15
goto loop
The problem in this small part of the script is that, the variable "a" cannot save the name of the folder, if i change the echo %a% to echo Hello World then the script prints only one time the message and not as long as the loop runs.
Any ideas?
I'm not sure if there are more bugs in the code but one problem is the missing of delayed expansion. Here is the fix:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:loop
for /f %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do (
SET a=%%i
echo !a!
)
echo Folder is empty or does not exist
timeout /t 15
goto loop
You have to add SETLOCAL ENABLEDELAYEDEXPANSION at the beginning of your script and replace %a% with !a!. You always to do this when changing a variable inside a for loop (and also in many other cases). For an explanation check http://ss64.com/nt/delayedexpansion.html
EDIT: replaced for /d ... with for /f ...