1.

Solve : Unusual error when trying to redirect output to a log file.?

Answer»

I am trying to copy files from one location to another USING XCOPY (across DIFFERENT servers using UNC Paths)
Then LOGGING the files copied. It works fine on my machine and the people around me here. But works differently when executed from a different location. We have offices in seperate buildings. So it works fine in My building, But does not in the other one.
Its not a permissions issue for sure.

Here's a part of the code(The entire one is too lengthy).

REM ****** MAP A DRIVE TO WORK WITH UNC PATHS**********
pushd %~dp0

set source="%cd%"

set logdir=C:\Logs
IF NOT EXIST "%logdir%" mkdir "%logdir%"

set today=%date:~10,4%%date:~4,2%%date:~7,2%
set target="\\Path\to\server"
for /f "tokens=* delims=" %%i in ('echo all ^| xcopy /l /d %source% %target% ^| find /v /i "overwrite"') do (
if exist %target%\%%~nxi (
copy %target%\%%~nxi %target%\%%~ni-%today%%%~xi >nul
echo %target%\%%~nxi >> "%logdir%\Code_Log-%today%.log"
)
)
echo all | xcopy /d %source% %target% >> "%logdir%\Code_Log-%today%.log"

IF EXIST "%logdir%\Code_Log-%today%.log" move "%logdir%\Code_Log-%today%.log" %target1%\Logs
IF EXIST "%logdir%" RMDIR /Q "%logdir%"

If I remove the Logging(redirecting the output to log files), It works perfectly fine on everyone's machine. But when I log the outputs, it works fine on my machine but not on others.

Very unusual according to me. I am stuck here for a while now, trying to figure out what the problem. Seems to be a small issue.

If you see Any unusual coding style where there are possibility of errors, would appreciate some information on that as well.
Any help is appreciated. That's it! I tired of writing responses only to have the CH connection time out. I'm taking my ball and going home.

Which line actually fails? Echo is a fairly transparent command, so logging a string to a file should not create a problem.

I know we don't have a full VIEW of you code, but where does target1 get a value from in this line:

IF EXIST "%logdir%\Code_Log-%today%.log" move "%logdir%\Code_Log-%today%.log" %target1%\Logs

Setting the source variable could be a problem, as not every machine is likely to have the same current directory.

Just a thought.

See thats the SURPRISING part.

The only issue here is: When I replace this part of the code:

echo %target%\%%~nxi >> "%logdir%\Code_Log-%today%.log"
)
)
echo all | xcopy /d %source% %target% >> "%logdir%\Code_Log-%today%.log"


with the following part:


echo %target%\%%~nxi )
)
echo all | xcopy /d %source% %target%


Basically getting rid of the logging, It works fine.

Also I am pushing the UNC Drive to map to any available drive , So for anyone who executes the script, the current drive would be the drive that gets mapped to it.

Was this confusing??

Thanks a lot SideWinder for the quick reply. Really appreciate it.I think I found the problem.

This is the reason I think. Although a small mistake but Its hard to find such issues when the actual program grows bigger.
This is what I read somewhere:

DIR>file.txt and DIR > file.txt are identical, ECHO Hello world>file.txt and ECHO Hello world > file.txt are not, even though they are both valid.

Its because of the space before and after the redirection.
Haven't tested this yet. As this problem does not occur on my machine or my building. Need to find someone from the other office to test it. So can't be 100% sure.Ok My Bad. I thought I had found the solution. But its still not working.

The problem is redirecting output to the log file. The moment I remove the lines I get the program working fine.

Unable to find a reason behind this. Is it something to do with mapping a Drive?
Is Pushd good to map to a drive or I should use something else?

I need to find a reason quick. Can someone help please?If you need to me post the entire code, let me know.
As you can see I am kinda getting desperate here. Could it be a question of write permissions?
Na. I dont think its a write permissions issue. But aneways I am first creating the Log file on one's local machine.. "C:\Logs" and then moving it over to the target location.

So the script should only fail in the last step. Apart from that XCOPY is also writing to the server, i.e creating new files if it does not exist. So I dont think there is a permissions issue here.

Any other Ideas??The problem is localized on your end. Try turning echo on in the machines where it fails. You'll have to see how the code runs and it may give you come insight as to the problem.

Just a hunch, but is the copycmd variable set on the machines where is fails? If so it may be preventing the code from entering the if statement.

Good luck. Thanks Sidewinder. Seems to be a problem that i am unable to understand. Works fine on my machine and the machines on my floor.

Let me try the solutions you gave me. Will post back If the problem gets solved.

Thanks again..

God Bless!!



Discussion

No Comment Found