I have managed to copy and manipulate a batch script that does the following:
looks at the last 4 characters of a .csv file
creates a folder with this name
moves the file into the folder
#echo off & setlocal EnableDelayedExpansion
echo.
pushd "%~dp0"
for %%j in (*.csv) do (
set file=%%~nj
set folder=!file:~-4!
if !folder!==FI_2 set folder=!file:~-6!
if not exist !folder! md !folder!
move "%%~j" "!folder!"
echo "%%~j" -^> "!folder!"
)
popd
echo.& echo.Done
:: End_Of_Batch
The problem I have is that I need to group the files by month. The file names look like this:
BS_IDX_LEVEL_YYYYMMDD_BAFI.csv
(obviously the YYYYMMDD will be replaced with the date of the file)
So any file with the name BS_IDX_LEVEL_20111231_BAFI.csv would go into a folder named "1112" (date formate of folder is YYMM)
A file with the name BS_IDX_LEVEL_20111115_BAFI.csv would go into a folder names "1111"
Is there someway I can alter this script so that before organising the files by their filenames it groups them by date first?
If I can explain anything further please let me know
While you are looping through and getting the last 4 characters of the file name, you can also look at the dates. I didn't know if the start of the file will be fixed or subject to change, but I thought it reasonable to assume the date_end syntax would be consistent.
set filedate=!file:~-9,-7!
This will extract the characters between 9th and 7th from the end of the String stored in !file!. With the syntax you have, the will be the two digit month number. This will also avoid any confusion with 2 or 4 digit dates.
Once you have both this and the other folder name, you can simply create both folders at once and place the file directly into the subfolder, so you will still only need to make one pass over the fileset.
#echo off & setlocal EnableDelayedExpansion
echo.
pushd "%~dp0"
for %%j in (*.csv) do (
set file=%%~nj
set folder=!file:~-4!
if !folder!==FI_2 set folder=!file:~-6!
set filedate=!file:~-9,-7!
SET datefolder=!folder!\!filedate!
if not exist !folder! md !folder!
if not exist !folder!\!filedate! md !folder!\!filedate!
move "%%~j" "!datefolder!"
echo "%%~j" -^> "!datefolder!"
)
popd
echo.& echo.Done
:: End_Of_Batch
Related
There are two folders holding equal amounts of files between them.
I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .
Input files are .bmp Output files are .ini ( has a gear symbol ).
Example :
folder 1- Flowers.bmp ; folder 2- blank.ini
. To this:
folder 1- Flowers.bmp ; folder 2- Flowers.ini
There would be more files , but equal .
The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.
Normally I have both folders situated on the Desktop .
I make sure both folders have equal number of files between them . That would be a constant .
I'm trying to stream line some menial repetitive daily tasks .
I did search and what I have found does not really help.
#ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF
:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL
Hoping to find a solution to this finally .
Ok, here is a slightly long version, but it makes sure it gets the content of each folder.
Note You must ensure that the path to both folders are correctly specified in the third and fourth line:
#echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
set /a num+=1
set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
set /a num+=1
set "to!num!r!=%%~nxb"
set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause
You can remove pause at the bottom of the script, once you are happy with the output.
This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).
#echo off
setlocal enabledelayedexpansion
(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"
<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
set /p "name="
ECHO ren "%%A" "!name!.ini"
))
The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.
The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.
I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.
I am looking for help to construct a .bat script to move PDFs into a predefined folder structure. The file names are structured and relate to where in the folder structure they should be moved to.
For example IRTYCAS001.pdf;
First two letters tell it to move it to the correct country folder (Ireland)
3rd and 4th will tell it what county folder to move it to
5th to 7th will tell it the correct town folder to move it to and
Last 3 digits tell it the land use type folder to move it to
The identifier lengths will always be the same in the file name.
The folder structure looks something like
example of folder structure
With extensions enabled (default) mkdir will create intermediate folders in one step.
So all you've to do is
iterate the files
use substrings to split the filename into parts and
create the folders if not already existing.
#echo off & setlocal EnableDelayedExpansion
set Src=A:\
set Dst=A:\
for /f "delims=" %%A in ('Dir /B "%Src%*.pdf"') do (
Set "File=%%A"
set "Folder=%Dst%\!File:~0,2!\!File:~2,2!\!File:~4,3!\!File:~7,3!\"
if not exist "!Folder!" MD "!Folder!" >NUL
Move "%%A" "!Folder!"
)
Sample tree:
> tree . /f
A:\
└───IR
└───TY
└───CAS
└───001
IRTYCAS001.pdf
you could try something like this:
FOR /F %%i IN ('dir /b c:\temp\*.pdf') DO call :moveFiles %%i
goto :EOF
:moveFiles
set myfile=%1
set part1=%myfile:~0,2%
set part2=%myfile:~2,2%
set part3=%myfile:~4,3%
set part4=%myfile:~7,3%
set dstFolder=C:\temp
if %part1%==IR set dstFolder=%dstFolder%\ireland
REM more options here...
if %part2%==TY set dstFolder=%dstFolder%\tipperary
REM more options here...
if %part3%==CAS set dstFolder=%dstFolder%\cashel
REM more options here...
if %part4%==001 set dstFolder=%dstFolder%\residential
REM more options here...
move /Y %myfile% %dstFolder%
First time poster and "very" limited experience. I have been tasked with taking PDF's (about 100,000+-) and sorting them. The file name is 123456700.PDF I would like to separate these files into folders no larger than 5,000 files. So depending on the 6th number in the file name, I would like to create a folder 123455000 (if 6th number is equal to or greater than 5) and 123450000 (if the 6th number is less than 5). Then I would like to move that file into the folder that was just created. I would like a Batch/Script File that could be ran on a selected folder that would accomplish this task. Thanks in advance for all replies!!
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
CD /D "X:\path\to\pdfs"
For %%A in (*.pdf) Do (
Set "Filename=%%~nA"
If !FileName:~5,1! lss 5 (
Set Folder=!FileName:~0,5!0000
) Else (
Set Folder=!FileName:~0,5!5000
)
If not Exist "%Folder%" MkDir "%Folder%"
Move %%A "%Folder%"
)
Edit substring position is zero based, had to change offset.
Similar to LotPings answer:
#Echo Off
Set "SrcDir=C:\Users\AName\Documents"
Set "DstDir=C:\Users\AName\Documents\PDFs"
If Not Exist "%SrcDir%\*.pdf" Exit/B
For %%A In ("%SrcDir%\*.pdf") Do (Set "FName=%%~nA"
SetLocal EnableDelayedExpansion
If !FName:~-4! Lss 5000 (Set DstNum=0000) Else Set "DstNum=5000"
If Not Exist "%DstDir%\!FName:~,-4!!DstNum!\" (
MD "%DstDir%\!FName:~,-4!!DstNum!")
Move "%%~A" "%DstDir%\!FName:~,-4!!DstNum!"
EndLocal)
I'm going to try and give this a crack, though I do not generally work with batch, but I can at least get you going...
for %%f in (*.pdf) do {
set TEMP = %f:5%
if exists <your path>/%TEMP% <do nothing>
if not exists <your path>/%TEMP% mkdir <your path>/%TEMP%
<move file to this new directory
}
This isn't entirely working/correct, but it should give you a good idea of what to do.
At work we use a bespoke program to search through a directory tree and find an individual image file. These files are stored in folders that have a 7-digit name, starting with '18' - so for instance '1873456', '1873457', '1873458' etc. The problem I have is at some point last year the procedure that creates these folders and populates the images in them reached '1899999' - and then rolled over to '18100000' and carried on like that for over 4,000 folders before it was caught and corrected.
The bespoke program we use can only handle seven-digit folder names. What I would like to do is create a batch file that renames all the eight-digit folders by removing the extra '1' in the name, so '18100000' becomes '1800000' and so forth until '18104013' becomes '1804013'.
Can anyone help?
Run this in the base folder, it will not change any folders.
A file called renfile.bat.txt will be created that contains the rename command for the folders that match the filespec. Examine it in notepad to see if it is ok and then rename it to renfile.bat and run it.
It's not tested.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (18??????) do (
set "name=%%~nxa"
>>renfile.bat.txt echo ren "%%a" "!name:~0,2!!name:~3!"
)
Something like
for /l %%x in (100000,1,104013) do (
set oldsuffix=%%x
set newsuffix=%oldsuffix:~-5%
ren 18%%x 18%newsuffix%
)
setlocal enableextensions enabledelayedexpansion
for /d /r "c:\somewhere" %%f in (181?????) do (
set "name=0" & set /a "name+=%%~nf" 2>nul
if !name! gtr 1899999 ren "%%~ff" "18!name:~-5!"
)
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")