简体   繁体   中英

Batch script works in XP and not in Win7

Utilizing the knowledge of these forums, I compiled a useful batch script.

Del "- Output.tx_"
Del "- Output.txt"

FOR %%I in (*.txt) do @echo Filename: %%~nxI >>"- Output.tx_" & echo. >>"- Output.tx_" & echo. >>"- Output.tx_" & FOR %%f in (%%I) do type %%I >>"- Output.tx_" & echo. >>"- Output.tx_" & echo. >>"- Output.tx_"

TYPE "- Output.tx_" | MORE /E /P /S > "- Output.txt"
Del "- Output.tx_"

My home computer has XP (SP3, 32-bit), and the script combines the text files perfectly as shown below.

Filename: Test1.txt  

[Text file 1 contents]

Filename: Test2.txt  

[Text file 2 contents] 

... and so on. 

However, I had run this script as a test on my computer at work a few months ago and ran into problems. The computer at work has Windows 7 (32-bit, SP1) and combined only the text files' titles into the output file. This resulted in the following:

Filename: Test1.txt  

Filename: Test2.txt     

... and so on. 

Any comments or feedback in getting the script to work properly on Windows 7 would be greatly appreciated.

Thanks,

DWIC

The only thing I can think of is that some (or all) of your text files on your Win7 computer have spaces or special characters in there names, such that TYPE is not able to find them. There should be an error message if this happens, but it is not in your output file because you haven't redirected stderr.

The problem can be solved by quoting the variable.

Your code is more complicated than it needs to be. Here is a simplified version that does the same thing, except the quoting problem has been fixed. I also redirected stderr.

@echo off
2>nul del "- Output.txt"
(
  for %%F in (*.txt) do @(
    echo Filename: %%F
    echo.
    echo.
    type "%%F"
    echo.
    echo.
  )
) | more /s /p >"- Output.tx_" 2>&1
move "- Output.tx_" "- Output.txt" >nul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM