Solved

Need help copying foldered files to thier respective folder based on a txt or xls list

Posted on 2015-02-23
7
141 Views
Last Modified: 2015-02-24
I have a situation where I have a document management server (3.8 million documents stored in 1900 folders) that has lost a bunch of files (thousands). What I do have is a huge list pulled from SQL in xls format that can be converted to text of all the files that were unceremoniously deleted by the system. What I also have is a backup of all 1900 folder and 2.8 million docs including the thousands of files that were deleted. What I need is a script that will copy all the files listed in the xls or txt file back to their correct folders. Here is an example of the txt file. This is an example of the text file with the file list:

\\cp1\CP_HISTORY\CPWin\History\041208_0024\This document is gone.doc
\\cp1\CP_HISTORY\CPWin\History\041208_0045\abc456.doc
\\cp1\CP_HISTORY\CPWin\History\041245_0001\Our DM server sucks.doc...etc

This is actually the destination path on the production server. Notice the server and first 3 directory levels do not change but the last directory and filename changes with each file.

Here's the pathing I am looking at with the backup data:

\\backuppc\e$\Backup 2015-02-08\CP1\History\041208_0024\This document is gone.doc
\\backuppc\e$\Backup 2015-02-08\CP1\History\041208_0045\abc456.doc
\\backuppc\e$\Backup 2015-02-08\CP1\History\041245_0001\Our DM server sucks.doc...etc

What I can't figure out is how to get a script to read the txt file with each file and path (thinking batch file with FOR loop but after that I have no idea), use this path and file name which is the destination path and name to copy the file from the backup path to the production path .

Any help would be extremely appreciated. Thanks
0
Comment
Question by:gwbmcse
  • 4
  • 3
7 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 40626187
Well, basically, a batch script could look like this (in test mode, it will only display the copy commands it would normally run; remove the uppercase ECHO in line 10 to run it for real). Whether this works depends mainly on the amount of files in the list, because "for /f" isn't really fast when it comes to large input files.
Make sure that the input file is saved as ANSI, NOT Unicode!
 
@echo off
setlocal enabledelayedexpansion
set FileList=C:\Temp\test.txt
set TargetRoot=\\cp1\CP_HISTORY\CPWin
set BackupRoot=\\backuppc\e$\Backup 2015-02-08\CP1
for /f "usebackq delims=" %%a in ("%FileList%") do (
	set Target=%%~a
	set Source=!Target:%TargetRoot%=%BackupRoot%!
	echo Processing '%%~a' ...
	ECHO copy "!Source!" "!Target!"
)

Open in new window

So just in case, here's a Powershell version; it's in test mode as well, remove the uppercase "-WHATIF" in line 9 to run it for real:
$FileList = "C:\Temp\test.txt"
$TargetRoot = "\\cp1\CP_HISTORY\CPWin"
$BackupRoot = "\\backuppc\e$\Backup 2015-02-08\CP1"
$TargetRoot = $TargetRoot.ToLower()
$BackupRoot = $BackupRoot.ToLower()
Get-Content -Path $FileList | % {
	"Processing '$($_)' ..." | Write-Host
	Try {
		Copy-Item -Path $_.ToLower().Replace($TargetRoot, $BackupRoot) -Destination $_ -Force -ErrorAction Stop -WHATIF
	} Catch {
		$_.Exception.Message | Write-Host -Fore Red
	}
}

Open in new window

0
 

Author Comment

by:gwbmcse
ID: 40626450
I tested it and it works but it's having trouble copying files with spaces in the filename. At least, that seems to be the common thread of the problem. Thanks for what you have done so far though, it works great for filename without a space.
0
 

Author Comment

by:gwbmcse
ID: 40626452
oh, I jumped directly to the powershell script. I didn't test the batch file.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 83

Expert Comment

by:oBdA
ID: 40626730
Neither the PS script nor the batch script should have issues with spaces in the file names, I tested with these kind of file names as well.
Could it be that the file names in the list have leading or trailing spaces?
Could it be that the "spaces" in the list aren't actually Char(32) / 0x20?
Are the file names in the list surrounded with quotes, and if so, with "pretty" ones, or the proper double quotes (as in Chr(34) / 0x22)?
If the lines in the file are enclosed in double quotes, use this for PS:
$FileList = "C:\Temp\test.txt"
$TargetRoot = "\\cp1\CP_HISTORY\CPWin"
$BackupRoot = "\\backuppc\e$\Backup 2015-02-08\CP1"
$TargetRoot = $TargetRoot.ToLower()
$BackupRoot = $BackupRoot.ToLower()
Get-Content -Path $FileList | % {
	"Processing '$($_)' ..." | Write-Host
	Try {
		Copy-Item -Path $_.Trim('"').ToLower().Replace($TargetRoot, $BackupRoot) -Destination $_.Trim('"') -Force -ErrorAction Stop -WHATIF
	} Catch {
		$_.Exception.Message | Write-Host -Fore Red
	}
}

Open in new window

0
 

Author Comment

by:gwbmcse
ID: 40626792
I did more testing and you're right, it's not the spaces. The problem is that all my doc names have a doc number inside brackets []...example doc name being: [1634] WCC Form 20.doc

If I remove the brackets, it works fine. Any workaround for the [ ]? Thanks again.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40627505
Sorry, that should have been "-LiteralPath" in Copy-Item, not "-Path":
$FileList = "C:\Temp\test.txt"
$TargetRoot = "D:\Temp" # "\\cp1\CP_HISTORY\CPWin"
$BackupRoot = "C:\Temp" # "\\backuppc\e$\Backup 2015-02-08\CP1"
$TargetRoot = $TargetRoot.ToLower()
$BackupRoot = $BackupRoot.ToLower()
Get-Content -Path $FileList | % {
	"Processing '$($_)' ..." | Write-Host
	Try {
		Copy-Item -LiteralPath $_.Trim('"').ToLower().Replace($TargetRoot, $BackupRoot) -Destination $_.Trim('"') -Force -ErrorAction Stop -WHATIF
	} Catch {
		$_.Exception.Message | Write-Host -Fore Red
	}
}

Open in new window

0
 

Author Closing Comment

by:gwbmcse
ID: 40628913
Work's perfectly. My company should write you a check as you have saved hundreds of man hours. Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now