Reputation: 1
I'm busy making a batch-script at my workplace that automatically syncs 10 different computers with a single network drive.
The network drive contains certain study folders and accompanying start menus for these studies, which it syncs to each individual computer. Example:
Study folder = D:\Studyfolder1234 Start menu = %USERPROFILE%\Desktop\Startmenu1234
I am trying to build a for loop that loops through folders on the computer, and syncs them accordingly. I accounted for the time needed to access the network drive by adding a latency in the batch script: 'ping -n 30 -w 1000 127.0.0.1 > nul'.
I want the batch script to automatically do the following:
After syncing, whenever there is a file present on the local computer that does NO LONGER exist on the network drive (e.g. Studyfolder5241), the local 'startmenu5241' is automatically moved to the local 'studyfolder5241' as a subfolder. After this I want the local 'studyfolder5241' to be moved to another local folder (an archive folder).
I'm a beginner when it comes to batch scripting, but I managed to get the xcopy commands to work:
(G:\= Network drive) (D:\= Local computer)
xcopy /s /d "G:\Startmenus\" "%USERPROFILE%\Desktop\" xcopy /s /d "G:\Study Folders\" "D:\"
Then I looked at 'for /D', but I find myself unable to compute a working variable based off the last 4 digits of the foldername (in my example it's the '5241'), which I believe should be the easiest way to accomplish an automated sync (as all our studyfolders and start menus end in similar strings of 4 digits). I also looked into the 'MOVE'command, which does look comprehensible, but I can only implement this after I figure out the correct conditional statement ('if folder exists locally, but not on network drive, then...')
Can anybody help me with this problem? If I am missing something, please point it out to me and I will try to collaborate on it.
Kind regards, Jurriaan
EDIT:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%F IN (%USERPROFILE%\desktop*) DO ( SET fold1=%%~nF ECHO Desktop !fold1:~-4!
IF NOT EXIST "D:\Folder1\Foldername followed by%fold1%" (
MOVE "%USERPROFILE%\desktop\Foldername followed by%fold1%" "D:\Foldername followed by%fold1%"
MOVE "D:\Foldername followed by%fold1%" "D:\Archive\"
) )
SETLOCAL DISABLEDELAYEDEXPANSION
cmd /k
Upvotes: 0
Views: 189
Reputation: 3025
So the answer is how to get some part of a folder's name? You cannot do this from xcopy but you can with for loop. Unfortunately due to CMD limitations the script is somewhat more complex than it should be:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%F IN (%CD%\*) DO (
SET fold=%%~nF
ECHO !fold:~-4!
)
SETLOCAL DISABLEDELAYEDEXPANSION
The script prints last 4 chars of all directories inside current path.
The catch is that part extraction doesn't work with loop variables. Moreover CMD needs special option to be set to change variable value inside loop.
Upvotes: 0