2

I have an xcopy that cannot run correctly in a .bat (batch) file because of some Norwegian characters.

The line is:

xcopy /I /V /H /R /C /Y /K /O /X "G:\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF" "F:\CAD\P - cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"

If I run it from a batch file it fails as "0 files copied" because it translates the source file incorrectly due to the character set.

I've tried changing the charsets using chcp as well as using Notepad++ and encoding it with various charsets but it still fails due to a limitation I guess in batch processing in Windows.

So my question is, how can I get the line above to execute in a vbscript and record the xcopy output?

I tried:

set objShell = CreateObject("wscript.Shell")
strSource = "G:\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
strDest = "F:\CAD\P - Tdwos_cad_files\Drawings\163997 Ø1000XØ90 T7-8-9.PDF"
command = "xcopy /I /V /H /R /C /Y /K /O /X " & chr(34) & strSource & chr(34) &chr(32) & chr(34)  & strDest & chr(34) & " > F:\testvbs.log 2>&1"
objshell.run command

and then execute "cscript test.vbs"

But unfortunately this doesn't work despite the fact that if I change the objshell.run to wscript.echo it does show the proper output for the "command" variable...meaning it shows the right syntax.

So...any programmers know how I can properly run that original xcopy line in a vbscript? If you know how to run it in a batch file properly with the encoding, that's fine too...I just haven't figured that out yet.

TheCleaner
  • 255
  • 1
  • 6
  • 18
  • Note: I should say I'm not even sure this will fix my encoding issue...but I'm hoping vbscript executes differently than the batch shell – TheCleaner Jun 10 '15 at 21:56
  • what if you use wildcards? – npocmaka Jun 10 '15 at 21:57
  • @npocmaka - I can't...I have a specific set of files (around 7k) that I need to copy from various sources to various destinations (1 for 1 match per file). I have the input source file list and destination file list, but that doesn't help me much. I tried a for loop but xcopy complained...but that gave me the same results. So I went for a "line for line xcopy batch file" but on any line with Norwegian characters it fails. – TheCleaner Jun 10 '15 at 22:00
  • Have you seen [this](http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how)? – Bond Jun 10 '15 at 22:25
  • @Bond - yes sir...I tried some chcp commands like that one and others but no dice – TheCleaner Jun 11 '15 at 00:07

2 Answers2

1

Next script works with combined Norwegian-Czech folder and file names under under next conditions:

  • save the script UTF-16LE encoded with the 0xFFFE byte order mark, and
  • run the script as administrator!

Here's commented code snippet:

' save the script UTF-16LE encoded with the 0xFFFE byte order mark
' run the script as administrator!
' consider //U option: Use Unicode for redirected I/O from the console
'  cscript //U 30767978.vbs  
'
option explicit
dim objShell, strSource, strDest, command, cmd, xx
set objShell = CreateObject("wscript.Shell")

strSource =   "D:\test\éíáýžřčšě\a Ø1000XØ90 b.txt"
strDest =     "D:\test\ěščřžýáíé\a Ø1000XØ90 b.txt"

'                      chcp does not matter: `dir` as well as `xcopy` work for all
cmd = "%comspec% /U /C chcp 65000 & "
cmd = "%comspec% /U /C chcp 65001 & "
cmd = "%comspec% /U /C chcp   437 & "
cmd = "%comspec% /U /C chcp   852 & "
cmd = "%comspec% /U /C chcp  1250 & "
'                      chcp supposedly matters for redirected output

command = "dir " & """" & strSource & """ """ & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)

command = "xcopy /I /V /H /R /C /-Y /K /O /X " _
    & """" & strSource & """ """  & strDest & """" & " & pause "
xx = objShell.Run( cmd & command, 1, true)

Wscript.Echo xx, cmd & command 
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Thanks. To be honest only tested your code with a single file but it does work. However, see my answer for what I actually ended up doing. I accepted yours because it was what the question asked...thank you and great job! – TheCleaner Jun 11 '15 at 13:12
0

While the accepted answer by JosefZ works well, I found that I could handle this much easier by simply taking my lines of xCopy and moving them from the .bat file to a Powershell .ps1 file and running it. Powershell doesn't balk at the character sets like Batch files do for some reason.

So I installed Powershell on the 2003 server and ran the script that way and it worked well.

I accepted JosefZ's answer since it answered the actual question, but posting this here in case others come across the same issue and want to utilize Powershell instead.

TheCleaner
  • 255
  • 1
  • 6
  • 18