0

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

1
  • Note that if delayed expansion for some variable was used, it must be used further too. So the loop might look something like DO ( SET fold=%%~nF SET FoldNum=!fold:~-4! MOVE "%USERPROFILE%\desktop\startmenu!FoldNum!" "%RemotePath%\studyfolder!FoldNum!") (here we extract folder number and then use it where needed, also plz check actual MOVE's parameters as it won't transfer folders AFAIR)
    – Fr0sT
    Jul 20, 2015 at 8:15

1 Answer 1

0

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.

1
  • Hey, thanks for the help! I got quite far with the script. The thing I can't figure out at this point is how to actually identify the folder and move them. I've updated my question with the new script. Would you be able to help me with this?
    – Jurtje
    Jul 14, 2015 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.