Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-02-23
7
Medium Priority
?
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 85

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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
LVL 85

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 85

Accepted Solution

by:
oBdA earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to count occurrences of each item in an array.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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