Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Files with absolute paths links - How to move them?

24 Jun 2015   #21
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Hey guys, I'm back,

And have your script, Videoclocknet.

I gave up scripting the solution in Batch after deciding the help of regex was sure needed to accomplish the task, and thus I’ve used VBScript instead.

Here are your instructions, Vid', create the VBScript that is attached to this post, whip out a Command Prompt and run a command line similar to the following,
Code:
C:\>cscript /nologo script.vbs "C:\Path\To\*.itb" "C:\Path\To\My\OutputFolder" /fileencoding:utf-16le /recurse

Please create a backup or set up a test environment to trial the script before attempting anything.


Preview
Code:
' th-373293.vbs InputFiles OutputFolder [/FileEncoding:UTF-8] [/RecurseInputFolder:False]

' Desc.: For each file specified by 'InputFiles', read value of the '<FileName>' 
'  tag in the file, move the file specified by the tag's value to 'OutputFolder' 
'  then edit the '<FileName>' tag's value to the new location of the moved file. 

Set objArgs = WScript.Arguments
Set objNamedArgs = WScript.Arguments.Named
Set fso = CreateObject("Scripting.FileSystemObject")
Set reFilemask = New RegExp
Set reXml = New RegExp

strOpenTag = "<FileName>"
strCloseTag = "</FileName>"

Sub EnforceCScriptUsage
	If Not LCase(Right(WScript.FullName, 11)) = "cscript.exe" Then
		MsgBox "Please use CScript to execute this script."
		WScript.Quit(1)
	End If
End Sub

Sub CheckSwitches
	If objNamedArgs.Count Then
		intCount = 0
		If (objNamedArgs.Exists("Recurse")) Or (objNamedArgs.Exists("RecurseInputFolder")) Or (objNamedArgs.Exists("R")) Then intCount = intCount + 1
		If (objNamedArgs.Exists("FileEncoding")) Or (objNamedArgs.Exists("E")) Then intCount = intCount + 1
		If Not objNamedArgs.Count = intCount Then
			WScript.Echo "Exception: unexpected switches used"
			WScript.Quit(1)
		End If
	End If
End Sub

Function ReadTextFile(strFileName, strCharSet)
	With CreateObject("ADODB.Stream")
		.Open
		.Charset = strCharSet
		.LoadFromFile strFileName
		ReadTextFile = .ReadText
		.Close
	End With
End Function

Function WriteTextFile(strFileName, strCharSet, strText)
	With CreateObject("ADODB.Stream")
		.Open
		.Charset = "utf-16le"
		.WriteText strText
		.SaveToFile strFileName, 2
		.Close
	End With
End Function

Function ProcessXmlFile(strXmlFileName)
	If Not strXmlFileName = WScript.ScriptFullName Then
			If reFilemask.Test(fso.GetFileName(strXmlFileName)) Then
				WScript.Echo vbCrLf & "Processing '" & strXmlFileName & "'..."
				strXmlContent = ReadTextFile(strXmlFileName, strFileEncoding)
				Set objMatchs = reXml.Execute(strXmlContent)
					For Each Match in objMatchs
						
						strXmlContent = ReadTextFile(strXmlFileName, strFileEncoding)
						XmlFileNameTag = Match.Value
						XmlFileNameTagValue = Mid(XmlFileNameTag, Len(strOpenTag) + 1, Len(XmlFileNameTag) - Len(strOpenTag) - Len(strCloseTag))
						On Error Resume Next
						'WScript.Echo "  " & "Moving '" & XmlFileNameTagValue & "'.."
						strNewFileName = fso.GetAbsolutePathName(strOutputFolder & "\" & fso.GetFileName(XmlFileNameTagValue))
						fso.MoveFile XmlFileNameTagValue, strNewFileName
						If Err <> 0 Then
							If Err = 53 Then WScript.Echo "  " & "Error: non-existing source file was referenced: '" & XmlFileNameTagValue & "'"
							If Err = 58 Then
								intCounter = 1
								strNewFileName = Left(fso.GetFileName(XmlFileNameTagValue), (Len(fso.GetFileName(XmlFileNameTagValue)) - (Len(fso.GetExtensionName(XmlFileNameTagValue)) + 1))) & " (" & intCounter & ")" & "." & fso.GetExtensionName(XmlFileNameTagValue)
								Do
									intCounter = intCounter + 1
									Err.Clear
									fso.MoveFile XmlFileNameTagValue, fso.GetAbsolutePathName(strOutputFolder & "\" & strNewFileName)
								Loop Until (Err = 0) Or (intCounter > 3000)
								If Err = 0 Then
									WScript.Echo "  " & "Note: source file '" & XmlFileNameTagValue & "' was renamed to '" & strNewFileName & "'"
								Else
									WScript.Echo "  " & "Unhandled Error: " & CStr(Err) & vbCrLf & "  " & "  " & "Failed to rename '" & XmlFileNameTagValue & "' to '" & strNewFileName & "'"
								End If
							End If 
						End If
						If Err = 0 Then
							strNewXmlContent = Replace(strXmlContent, XmlFileNameTag, strOpenTag & strNewFileName & strCloseTag)
							On Error Goto 0
							WriteTextFile strXmlFileName, strFileEncoding, strNewXmlContent
						End If							
						On Error Goto 0
						
					Next
			End If
		End If
End Function

EnforceCScriptUsage()

Select Case objArgs.Unnamed.Count
	Case 0
		WScript.Echo "Exception: missing 2 required positional arguments: 'InputFiles' and 'OutputFolder'"
		WScript.Quit(1)
	Case 1
		WScript.Echo "Exception: missing 1 required positional argument: 'OutputFolder'"
		WScript.Quit(1)
	Case 2
		strInputFiles = WScript.Arguments.Item(0)
		strOutputFolder = WScript.Arguments.Item(1)
		strInputFolder = fso.GetParentFolderName(fso.GetAbsolutePathName(strInputFiles))
		strInputFilemask = fso.GetFileName(strInputFiles)
		strInputFilemaskRegex = strInputFilemask
		strInputFilemaskRegex = Replace(strInputFilemaskRegex, ".", "\.")
		strInputFilemaskRegex = Replace(strInputFilemaskRegex, "?", ".")
		strInputFilemaskRegex = Replace(strInputFilemaskRegex, "*", ".*")
		strInputFilemaskRegex = "^" & strInputFilemaskRegex & "$"
	Case Else
		MsgBox "Exception: this script takes exactly 2 arguments but " & objArgs.Count & " were given"
		WScript.Quit(1)
End Select

CheckSwitches()

If (objNamedArgs.Exists("FileEncoding")) And (objNamedArgs.Exists("E")) Then
	WScript.Echo "Exception: 'FileEncoding' optional parameter cannot be used twice"
	WScript.Quit(1)
ElseIf (objNamedArgs.Exists("FileEncoding")) Or (objNamedArgs.Exists("E")) Then
	If objNamedArgs.Exists("FileEncoding") Then strFileEncoding = objNamedArgs.Item("FileEncoding")
	If objNamedArgs.Exists("E") Then strFileEncoding = objNamedArgs.Item("E")
Else
	strFileEncoding = "UTF-8"
End If

reFilemask.Global = True
reFilemask.IgnoreCase = True
reFilemask.Pattern = strInputFilemaskRegex

reXml.Global = True
reXml.IgnoreCase = False
reXml.Pattern = strOpenTag & ".+?" & strCloseTag

If Not fso.FolderExists(strOutputFolder) Then
	WScript.Echo "Exception: the output folder '" & fso.GetAbsolutePathName(strOutputFolder) & "' does not exist"
	WScript.Quit(1)
End If

If (objNamedArgs.Exists("Recurse")) Or (objNamedArgs.Exists("RecurseInputFolder")) Or (objNamedArgs.Exists("R")) Then
	Function RecurseFolders(objFolder)
		For Each objFile In objFolder.Files
			
			ProcessXmlFile(objFile.Path)
			
		Next
	For Each strSubFolder In objFolder.SubFolders
			RecurseFolders(strSubFolder)
		Next
	End Function
	RecurseFolders fso.GetFolder(strInputFolder)
Else
	For Each objFile In fso.GetFolder(strInputFolder).Files
		
		ProcessXmlFile(objFile.Path)
		
	Next
End If





Attached Files
File Type: vbs th-373293.vbs (6.2 KB, 1 views)
My System SpecsSystem Spec
.
24 Jun 2015   #22
videoclocknet

Windows 7 32 bits
 
 

Ey my friend,

I'm so glad of your support. I'm going to test that script. And then I will explain the results on the forum

Cheers
My System SpecsSystem Spec
24 Jun 2015   #23
videoclocknet

Windows 7 32 bits
 
 

Ey guys,

The script that Pyprohly has coded works great. I've made a little change because I prefer to copy the images to the new folder instead of moving them. In that way, I keep the original itf files and images in case of an error.


That said I've changed the two coincidences of
Code:
MoveFile
function to
Code:
CopyFile
.

So now the code is as follows:

Code:
' th-373293.vbs InputFiles OutputFolder [/FileEncoding:UTF-8] [/RecurseInputFolder:False]

' Desc.: For each file specified by 'InputFiles', read value of the '<FileName>' 
'  tag in the file, move the file specified by the tag's value to 'OutputFolder' 
'  then edit the '<FileName>' tag's value to the new location of the moved file. 

Set objArgs = WScript.Arguments
Set objNamedArgs = WScript.Arguments.Named
Set fso = CreateObject("Scripting.FileSystemObject")
Set reFilemask = New RegExp
Set reXml = New RegExp

strOpenTag = "<FileName>"
strCloseTag = "</FileName>"

Sub EnforceCScriptUsage
	If Not LCase(Right(WScript.FullName, 11)) = "cscript.exe" Then
		MsgBox "Please use CScript to execute this script."
		WScript.Quit(1)
	End If
End Sub

Sub CheckSwitches
	If objNamedArgs.Count Then
		intCount = 0
		If (objNamedArgs.Exists("Recurse")) Or (objNamedArgs.Exists("RecurseInputFolder")) Or (objNamedArgs.Exists("R")) Then intCount = intCount + 1
		If (objNamedArgs.Exists("FileEncoding")) Or (objNamedArgs.Exists("E")) Then intCount = intCount + 1
		If Not objNamedArgs.Count = intCount Then
			WScript.Echo "Exception: unexpected switches used"
			WScript.Quit(1)
		End If
	End If
End Sub

Function ReadTextFile(strFileName, strCharSet)
	With CreateObject("ADODB.Stream")
		.Open
		.Charset = strCharSet
		.LoadFromFile strFileName
		ReadTextFile = .ReadText
		.Close
	End With
End Function

Function WriteTextFile(strFileName, strCharSet, strText)
	With CreateObject("ADODB.Stream")
		.Open
		.Charset = "utf-16le"
		.WriteText strText
		.SaveToFile strFileName, 2
		.Close
	End With
End Function

Function ProcessXmlFile(strXmlFileName)
	If Not strXmlFileName = WScript.ScriptFullName Then
			If reFilemask.Test(fso.GetFileName(strXmlFileName)) Then
				WScript.Echo vbCrLf & "Processing '" & strXmlFileName & "'..."
				strXmlContent = ReadTextFile(strXmlFileName, strFileEncoding)
				Set objMatchs = reXml.Execute(strXmlContent)
					For Each Match in objMatchs
						
						strXmlContent = ReadTextFile(strXmlFileName, strFileEncoding)
						XmlFileNameTag = Match.Value
						XmlFileNameTagValue = Mid(XmlFileNameTag, Len(strOpenTag) + 1, Len(XmlFileNameTag) - Len(strOpenTag) - Len(strCloseTag))
						On Error Resume Next
						'WScript.Echo "  " & "Moving '" & XmlFileNameTagValue & "'.."
						strNewFileName = fso.GetAbsolutePathName(strOutputFolder & "\" & fso.GetFileName(XmlFileNameTagValue))
						fso.CopyFile XmlFileNameTagValue, strNewFileName
						If Err <> 0 Then
							If Err = 53 Then WScript.Echo "  " & "Error: non-existing source file was referenced: '" & XmlFileNameTagValue & "'"
							If Err = 58 Then
								intCounter = 1
								strNewFileName = Left(fso.GetFileName(XmlFileNameTagValue), (Len(fso.GetFileName(XmlFileNameTagValue)) - (Len(fso.GetExtensionName(XmlFileNameTagValue)) + 1))) & " (" & intCounter & ")" & "." & fso.GetExtensionName(XmlFileNameTagValue)
								Do
									intCounter = intCounter + 1
									Err.Clear
									fso.CopyFile XmlFileNameTagValue, fso.GetAbsolutePathName(strOutputFolder & "\" & strNewFileName)
								Loop Until (Err = 0) Or (intCounter > 3000)
								If Err = 0 Then
									WScript.Echo "  " & "Note: source file '" & XmlFileNameTagValue & "' was renamed to '" & strNewFileName & "'"
								Else
									WScript.Echo "  " & "Unhandled Error: " & CStr(Err) & vbCrLf & "  " & "  " & "Failed to rename '" & XmlFileNameTagValue & "' to '" & strNewFileName & "'"
								End If
							End If 
						End If
						If Err = 0 Then
							strNewXmlContent = Replace(strXmlContent, XmlFileNameTag, strOpenTag & strNewFileName & strCloseTag)
							On Error Goto 0
							WriteTextFile strXmlFileName, strFileEncoding, strNewXmlContent
						End If							
						On Error Goto 0
						
					Next
			End If
		End If
End Function

EnforceCScriptUsage()

Select Case objArgs.Unnamed.Count
	Case 0
		WScript.Echo "Exception: missing 2 required positional arguments: 'InputFiles' and 'OutputFolder'"
		WScript.Quit(1)
	Case 1
		WScript.Echo "Exception: missing 1 required positional argument: 'OutputFolder'"
		WScript.Quit(1)
	Case 2
		strInputFiles = WScript.Arguments.Item(0)
		strOutputFolder = WScript.Arguments.Item(1)
		strInputFolder = fso.GetParentFolderName(fso.GetAbsolutePathName(strInputFiles))
		strInputFilemask = fso.GetFileName(strInputFiles)
		strInputFilemaskRegex = strInputFilemask
		strInputFilemaskRegex = Replace(strInputFilemaskRegex, ".", "\.")
		strInputFilemaskRegex = Replace(strInputFilemaskRegex, "?", ".")
		strInputFilemaskRegex = Replace(strInputFilemaskRegex, "*", ".*")
		strInputFilemaskRegex = "^" & strInputFilemaskRegex & "$"
	Case Else
		MsgBox "Exception: this script takes exactly 2 arguments but " & objArgs.Count & " were given"
		WScript.Quit(1)
End Select

CheckSwitches()

If (objNamedArgs.Exists("FileEncoding")) And (objNamedArgs.Exists("E")) Then
	WScript.Echo "Exception: 'FileEncoding' optional parameter cannot be used twice"
	WScript.Quit(1)
ElseIf (objNamedArgs.Exists("FileEncoding")) Or (objNamedArgs.Exists("E")) Then
	If objNamedArgs.Exists("FileEncoding") Then strFileEncoding = objNamedArgs.Item("FileEncoding")
	If objNamedArgs.Exists("E") Then strFileEncoding = objNamedArgs.Item("E")
Else
	strFileEncoding = "UTF-8"
End If

reFilemask.Global = True
reFilemask.IgnoreCase = True
reFilemask.Pattern = strInputFilemaskRegex

reXml.Global = True
reXml.IgnoreCase = False
reXml.Pattern = strOpenTag & ".+?" & strCloseTag

If Not fso.FolderExists(strOutputFolder) Then
	WScript.Echo "Exception: the output folder '" & fso.GetAbsolutePathName(strOutputFolder) & "' does not exist"
	WScript.Quit(1)
End If

If (objNamedArgs.Exists("Recurse")) Or (objNamedArgs.Exists("RecurseInputFolder")) Or (objNamedArgs.Exists("R")) Then
	Function RecurseFolders(objFolder)
		For Each objFile In objFolder.Files
			
			ProcessXmlFile(objFile.Path)
			
		Next
	For Each strSubFolder In objFolder.SubFolders
			RecurseFolders(strSubFolder)
		Next
	End Function
	RecurseFolders fso.GetFolder(strInputFolder)
Else
	For Each objFile In fso.GetFolder(strInputFolder).Files
		
		ProcessXmlFile(objFile.Path)
		
	Next
End If
It includes the subfolders in the search -I've seen the changes.
At the moment I've tested it from a testing environment and I can confirm that after running the script, the logo appears when I open the itf file from the labeling software. When viewing the .itf files, the path is correctly changed.


Congratulations, Pyprohly
My System SpecsSystem Spec
.

24 Jun 2015   #24
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Thank you. You're welcome, I'm pleased it worked great for you.

Ey, and welcome to Seven Forums too, Videoclocknet.
My System SpecsSystem Spec
Reply

 Files with absolute paths links - How to move them?




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Odd Files Left Over After SP1 - Can't Delete (Long Paths)
Hi, After installing installing SP1, a folder on my C drive is left over. I am unable to delete it since the path is too long, see attachment. Also, see attached txt file for directory and file info. I can't rename the files either, rename does not even show up on context menu and F2...
Installation & Setup
Dos paths and unix paths conversion
I guess this may be a tiny bit off the subject here, but I am attempting this in windows 7. I have a program file manager tool called 'Directory Opus' that is capable of calling windows external programs and passing them the current directory the file manager is sitting on. So far so good. ...
General Discussion
EasyBCD: How to create absolute paths
Hi, My bootable NTFS Test Volume contains just the bootmgr.exe and the G:\boot\BCD file. In the BCD I got something like this: device: ramdisk=\sources\BOOT.WIM,{99051b9e-c748-11e1-b1c7-14dae9e74180} which is a Relative path with its root in G:\ However, this path is wrong as...
Installation & Setup
Can't move folders with files, but can move files
I am attempting to move folders and files around, doing a little organization on my HDD. However if I attempt to move the loaded folder I get the error message " The action can't be completed because the folder or a file in it is open in another program." However I have no other programs open,...
General Discussion
Move Program FIles to SSD - Leave Users files Behind
Greetings Again, My file and storage system is set up like this: C:\ Drive - 120 GB SSD This is my Win 7 64-Bit Ultimate Boot drive. It has my most important Microsoft programs on it. Out of the 111GB available, I've used 42GB, leaving me 69GB free space. I want to keep this area open. ...
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 16:00.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App