1

We are re doing a folder structure on our main server for storage. But we want to simplify the process as much as possible for the graphics design team that's going to use it. So I am writing a bunch of scripts and this specific script needs to create a new folder for the year specified, rename the previous years fold and the the oldest year folder needs to be moved to Archive & renamed. Folder Structure

@ECHO OFF

SET /P nyear=Enter Year:
SET /A myear =%nyear%-1
SET /A ayear =%nyear%-2

mkdir "2. "%nyear%
rename "2. "%myear% "3. "%myear%
move "3. "%ayear% %cd%\Archive\
rename %cd%\Archive\"3. "%ayear% %ayear%

SET RootDir=%cd%\2. %nyear%
echo %RootDir%
SET SubA="1. January","2. February","3. March","4. April","5. May","6. June", "7. July", "8. August" "9. September", "10. October", "11. November", "12. December" 
SET SubB="1. Campaigns","2. Ads","3. Internal","4. Facebook","5. Instagram"
FOR %%A IN (%SubA%) DO FOR %%B IN (%SubB%) DO IF NOT EXIST "%RootDir%\%%~A\%%~B" MD "%RootDir%\%%~A\%%~B"
EXIT

I have tested the individual parts of the script and they work fine but when it runs as a whole I get the following error:

The process cannot access the file because it is being used by another process. 

error message

Any help would be appreciated.

  • 1
    Always quote whole paths, not only parts of it: e. g., `mkdir "2. %nyear%"`… – aschipfl Jun 18 '20 at 16:57
  • Why must the year be entered manually. The current year can be determined by the batch file itself and so also the previous year. See [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/) and [Time is set incorrectly after midnight](https://stackoverflow.com/questions/60124351/). The last referenced question has two answers explaining the most often used methods to get current date/time very detailed. – Mofi Jun 18 '20 at 17:01
  • 1
    I strong recommend to use the international date format which is `yyyy-MM-dd`. It has many advantages in comparison to all other formats. The first one is that it is standardized for entire world. The second one is that everybody understands this format. The third is that files/folders sorted alphabetically by name as by default with date in name in international format are sorted at the same time chronological. The fourth advantage is that a string comparison can be done with comparison operator `GTR` or `LSS` to find out which date is older. – Mofi Jun 18 '20 at 17:07
  • Well, aschipfl has written already about main syntax error resulting in this batch file not working. I recommend to read the __issues__ chapters of [this answer](https://stackoverflow.com/a/60686543/3074564) to get more knowledge on how to code a batch file with correct syntax to avoid the mistakes which are made by beginners in batch file coding. – Mofi Jun 18 '20 at 17:11

0 Answers0