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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Bill PrewIT / Software Engineering ConsultantCommented:
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
NVITEnd-user supportCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.