I've got quite a lot of files I am trying to archive for the company I work for. I am a little familiar with Batch scripts, but I do not quite understand enough to fully get what I am after.
I am trying to copy some files based on the beginning of their file name into folders on our NAS. The files are 7z files and their structure is going to be like so:
- 5476 BMW Handle-A.7z
- 5487 Chevy-Imp.7z
- 5986 Honda Lid-Upper.7z
etc.
The file structure works like this: the four numbers at the beginning is our company job number. On our NAS we have an archive directory with folders in it like this:
- _5000-5999
- _6000-6999
And inside of those folders are folders that will contain 250 archive files each. They are formatted like this:
- _5000-5249
- _5250-5499
- _5500-5749
- _5750-5999
I am looking to create a Batch file that I can drag these 7z files onto and it will read the first four numbers of the file name, and copy it to the proper folder on our NAS.
So for instance the files:
- 5476 BMW Handle-A.7z
- 5487 Chevy-Imp.7z
Would copy into
\nas01\archive\ _5000-5999\ _5250-5499
etc.
The main code I was messing with was this:
@echo off
for /f "delims=" %%i in ('dir /b /a-d *.7z') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~0,4!"
mkdir "!folder1!" 2>nul
copy "!filename1!" "!folder1!" >nul
)
What is not working for me is this line:
set "folder1=!filename1:~0,4!"
How can I create some sort of variable to check the file name, create the folders if necessary and copy it to the correct folder? I would appreciate any help on this!
-Dustin