Jimushi
Jimushi

Reputation: 121

How can I use a batch file to selectively delete text in text files?

@ECHO OFF

if not exist "C:\test\test.txt" (
    goto end
    ) else (
    goto loop
    )

:loop

echo Insert the name of the folder:
set /p name=<"C:\test\test.txt"

for /F "skip=1 delims=" %%a in ('type "C:\test\test.txt" ^& del "C:\test\test.txt"') do >> "C:\test\test.txt" echo %%a

echo Insert the name of the subfolder: 
set /p name2=<"C:\test\test.txt"

for /F "skip=1 delims=" %%a in ('type "C:\test\test.txt" ^& del "C:\test\test.txt"') do >> "C:\test\test.txt" echo %%a

md "C:\test\%name%\testing %name2% 999"

move "C:\test\*%name2%*.txt" "C:\test\%name%\testing %name2% 999"
if exist "C:\test\test.txt" goto loop

:end

pause
exit

I want to make an "if" before the "echo Insert the name of the folder:" part, so that if the 1st line of the "test" text file contains any of this characters "\ / : * ? " < > |" it will delete those special characters

Upvotes: 0

Views: 564

Answers (2)

Mofi
Mofi

Reputation: 49097

It looks like the file C:\test\test.txt should contain a set of folder names with on odd lines the main folder name and on even lines the subfolder name without path.

Microsoft explains on documentation page about Naming Files, Paths, and Namespaces which characters are not allowed in file/folder names.

If a line in the text file with the folder names contains an invalid character for a folder name, removing the character is not really the solution as it is most likely of no real help to move the files which contain the subfolder name into the subfolder.

I suggest following batch file for this task:

@echo off
if not exist "C:\test\test.txt" goto end
setlocal EnableExtensions DisableDelayedExpansion
set "MainFolderName="
set "SubfolderName="

for /F "usebackq eol=| delims=" %%I in ("C:\test\test.txt") do (
    if not defined MainFolderName (
        set "MainFolderName=%%~nxI"
    ) else (
        set "SubfolderName=%%~nxI"
        setlocal EnableDelayedExpansion
        md "C:\test\!MainFolderName!\testing !SubfolderName! 999" 2>nul
        if exist "C:\test\!MainFolderName!\testing !SubfolderName! 999\" (
            move "C:\test\*!SubfolderName!*.txt" "C:\test\!MainFolderName!\testing !SubfolderName! 999\"
            rd "C:\test\!MainFolderName!\testing !SubfolderName! 999\" 2>nul
        )
        endlocal
        set "MainFolderName="
        set "SubfolderName="
    )
)
endlocal

:end
pause

I do not recommend using command exit in a batch file, especially not at end of the batch file. This is not useful and has just the disadvantage that debugging the batch file becomes a nightmare.

The command FOR with option /F and the other options in double quotes processes the text file line by line with skipping empty lines and lines starting with |.

The first string read from a line after last \ or / is assigned to variable MainFolderName.

The second string read from a line after last \ or / is assigned to variable SubfolderName.

Next delayed expansion is enabled to be able to reference the string values of the two environment variables.

The directory is created with suppressing an error output by redirecting it from handle STDERR to device NUL. An error is output if folder or subfolder name contains an invalid character.

The IF condition checks, if the directory really exists which is not the case on invalid character in one of the two directory names. So the command MOVE is executed only on valid folder names and command RD removes the directory on being still empty after moving the files.

Then the two environment variables are deleted before processing the next two lines from text file.

It would be possible to process the lines read from text file as described for example at:

How to verify if variable contains valid filename in Windows Batch

But I think, this is not really necessary. The best folder name verification is done by the file system itself. So removing all characters from a line read from a text file which are not allowed in a folder name is not really needed in this case.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • pause /?
  • rd /?
  • set /?
  • setlocal /?

Read also the Microsoft article about Using command redirection operators for an explanation of 2>nul.

Upvotes: 2

Hackoo
Hackoo

Reputation: 18837

You can remove special characters with a batch script using regex in vbscript : Demo Here

@echo off
Color 0A
Title How to verify if variable contains valid filename in Windows Batch
echo(
Echo Enter filename for this project
set /p "my_filename="
echo(
echo Before Removing the special char the filename is like this : "%my_filename%"
pause
echo(
Call :Remove_Special_Char "%my_filename%" NewFileName
echo After Removing the special char the filename becomes like this : "%NewFileName%"
pause & exit
::-----------------------------------------------------------------------------------
:Remove_Special_Char <String> <Variable to Set>
(
    echo WScript.StdOut.WriteLine Search_Replace(Data^)
    echo Function Search_Replace(Data^)
    echo Dim strPattern, strReplace, strResult,oRegExp
    echo Data = wscript.Arguments(0^) 
    echo strPattern = "[\\\/:*?\x22<>|]"
    echo strReplace = ""
    echo Set oRegExp = New RegExp
    echo oRegExp.Global = True 
    echo oRegExp.IgnoreCase = True 
    echo oRegExp.Pattern = strPattern
    echo strResult = oRegExp.Replace(Data,strReplace^)
    echo Search_Replace = strResult
    echo End Function
)>"%tmp%\%~n0.vbs"
@For /f "delims=" %%i in ('cscript //nologo "%tmp%\%~n0.vbs" "%~1"') do ( Set "%2=%%i" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------

Upvotes: 0

Related Questions