0

ok I am trying to strip the first two characters from a file I am using this script.

@echo off
Set "InputFile=C:\New Folder\test.txt"
Set "OutputFile=C:\New Folder\New\test.txt"

setLocal EnableDelayedExpansion > "%OutputFile%"

for /f "usebackq tokens=* delims= " %%a in ("%InputFile%") do (
set s=%%a
>> "%OutputFile%" echo.!s:~2!
)

which works perfect if I use the correct name. What I need to do is use a wild characters since the name of the file is different each time. When trying this it does not work.

@echo off
Set "InputFile=C:\New Folder\H*.txt"
Set "OutputFile=C:\New Folder\New\H*.txt"

setLocal EnableDelayedExpansion > "%OutputFile%"

for /f "usebackq tokens=* delims= " %%a in ("%InputFile%") do (
set s=%%a
>> "%OutputFile%" echo.!s:~2!
)
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • this will remove the first two characters from every line in your file. Is this, what you want? – Stephan Jan 15 '14 at 17:26

2 Answers2

0

from

I'd like to use a wildcard with the SET command in Windows Batch so I don't have to know exactly what is in the string in order to match it

The asterisk IS a wildcard and WILL match multiple characters, but will ONLY match everything from the very beginning of the string. Not in the middle, and not from the end.

Useful Searches:

*x
*how are you? The above two searches CAN be matched. The first will match everything up to and including the first "x " it runs across. The second one will match everything up to and including the first "how are you?" it finds.

Legal, but Unuseful, searches:

x* Hello* OneThree The above three searches can NEVER be matched. Oddly they are also legal, and will cause no errors. One exception: Hello and x* WILL match themselves, but only if they are the very beginning of the string. (Thanks Jeb!)

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
0

the surrounding for-loop does the wildcard processing (giving full qualified filenames)

@echo off
for /f %%i in ('dir /b C:\New Folder\H*.*') do (
 echo processing %%i

  Set "InputFile=C:\New Folder\%%i"
  Set "OutputFile=C:\New Folder\New\%%i"

  setLocal EnableDelayedExpansion > "%OutputFile%"

  for /f "usebackq tokens=* delims= " %%a in ("%InputFile%") do (
    set s=%%a
    >> "%OutputFile%" echo.!s:~2!
  )
  endlocal
)

Note: if you have more than one line in those files, it will remove the first two characters from each line*.

Stephan
  • 53,940
  • 10
  • 58
  • 91