Script to Restore Files Based File Name

Hello,
We accidentally deleted files. We have a backup but it is not reliable and did not backup these files properly. A lot of them are missing. We do happened to have these missing files archived in another location. I would like to see if we can automate a restore from this archived location.

The main production network share has a structure of "C:\FTP Folder\PO#\Date\PO#_Item#\League_Item#_Team_Player_Color\". To find this path in the "archived" location we need to use the last folder in the main production path (ex: League_Item#_Team_Player_Color). So, after each underscore is another folder in the archived location. So we would go to the archived location and it would be "F:\League\Item#\Team\Player\Color". Is there a script that we can make to pull the files from the archived location to the main production share?

Not sure if this is possible via a script, but that is what I am hoping for.

Thank You!!
eshiramAsked:
Who is Participating?
 
Steven CarnahanNetwork ManagerCommented:
Missed a little.  Try this one:

echo off
set str="Folder\PO#\Date\PO#_Item#\League_Item#_Team_Player_Color\"
set str2=%str:Folder\PO#\Date\PO#_Item#\=F:\%
set str3=%str2:_=\%
echo %str3%
pause

Open in new window

0
 
KromptonCommented:
You asked for a way to automate this; is this because of the number of files or are you expecting to have to repeat this procedure?

For a one-time restore, XCopy or RoboCopy may suit your needs.

Krompton
0
 
Steven CarnahanNetwork ManagerCommented:
Try this batch file:

echo off
set str="Folder\PO#\Date\PO#_Item#\League_Item#_Team_Player_Color\"
set str2=%str:Folder\PO#\Date\PO#_=F:\%
set str3=%str2:_=\%
echo %str3%
pause

Open in new window

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Bill PrewCommented:
So, you want to look at each of these folders in the main server location, and then do what?  Copy all files from the backup location?  What if they already exist in the main server, overwrite, or skip?

~bp
0
 
NVITCommented:
REVISED:

If you need to process multiple folders in source folder c:\ftp, try this.

Note:
- I've placed an ECHO so you can see what the command does. It does not do the actual copy. To run it for real, remove the ECHO
- I used XCOPY command. Of course, use whatever command you like.
echo off
setlocal enabledelayedexpansion

set arch=C:\FTP Folder\PO#\Date\PO#_Item#
set patt=*

set targetdir=c:\targetdir\

pushd %arch%
for /d %%a in (%patt%) do (
  set str=%%a
  set str2=!str:_=\!
  ECHO xcopy /y /i /e "f:\!str2!" "%targetdir%"
)
popd
goto :eof

Open in new window

0
 
Bill PrewCommented:
Give this BAT file a test and see if it could be a starting point.  It will look at the folders in the master, and build the corresponding name for the location in the backup for the files, and then copy them over.

Right now it won't actually copy anything, but it will display the COPY command that it would execute to the console, so you can run it and see if what is copying is what you want.  If so remove the ECHO before the COPY and run for real.

It won't overwrite currently, but we can add an option to the COPY if you want that.

@echo off
setlocal EnableDelayedExpansion

REM "C:\FTP Folder\PO#\Date\PO#_Item#\League_Item#_Team_Player_Color\"
REM "F:\League\Item#\Team\Player\Color"

set BaseDir=C:\FTP Folder
set BackupDir=F:

for /d %%A in ("%BaseDir%\*.*") do (
  for /d %%B in ("%%~A\*.*") do (
    for /d %%C in ("%%~B\*.*") do (
      for /d %%D in ("%%~C\*.*") do (
        set BackupPath=%%~nD
        set BackupPath=!BackupPath:_=\!
        ECHO copy "%BackupDir%\!BackupPath!\*.*" "%%~D"
      )
    )
  )
)

Open in new window

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.