-1

how to remove hyphen "-" in a batch of document that does not have a consistence structure.

eg
1XXXXXX RevX - XXX
1XXXXXX RevX (this file dont have -)
1XXXXXX RevX - XXXXX
1XXXXXX RevXX - XXXXX
intended output
1XXXXXX RevX XXX
1XXXXXX RevX 
1XXXXXX RevX XXXXX
1XXXXXX RevXX XXXXX

I have tried ren "*-*" "*/*" but it is not working. Please advise. Thanks

SoraHeart
  • 400
  • 3
  • 14
  • Well, `"*/*` is not part of a legal filename in Windows, because you can't have a forward slash in a filename. Try something else instead, like renaming to a valid filename. – Ken White Jan 14 '21 at 02:50
  • I read else where that the / is to replace `-` with `space`. I don;t need the `/`, i just want `-` to be removed. – SoraHeart Jan 14 '21 at 02:58
  • https://stackoverflow.com/q/4429604/62576 shows a way to do it in VBScript. You can't do that with a simple `ren` command. – Ken White Jan 14 '21 at 03:11

1 Answers1

0

As already commented, you can't simply use ren with wildcards here.
Therefore you have to process each file on its own. cmd provides the for loop to do so:

for %%a in (*-*) do call :removedash "%%a"
goto :eof

:removedash
set "name=%~n1"          &REM take name only            
set "name=%name:-=%"     &REM remove dashes
set "name=%name:  = %"   &REM replace double-spaces with single-space
ECHO ren "%~1" "%name%%~x1"

Note: if you have a space both before and after the dash, removing the dash results in two consecutive spaces, which you probably don't want, so we replace them with a single space. We could just replace <space><dash><space> with <space>, but I'm not sure if we can trust there are spaces around the dash.

NOTE: I "disarmed" the ren command for security reasons. Remove the ECHO when you are sure it does what you want.

Stephan
  • 53,940
  • 10
  • 58
  • 91