-1

The file is not added in the destination path if it has spaces in the file name.

For example, if the filename is textfile1.txt -> it will be added in the destination path. However, if the directory filename has space like this text file4.txt it will not be added.

Is there a way to remove the spaces of the filename? Here is the image:

Kindly check the image here

Here is my main concern:

Check this

Here is my script:

@ECHO off
TITLE (c) ASDG 
SETLOCAL EnableDelayedExpansion

SET locationPath=C:\Textfiles\
SET destinationPath=E:\Textfiles\
SET status=success
SET countMetadata=0
SET countPDF=0
SET countJPEG=0

ECHO Executing the program...
FOR /R %locationPath% %%g IN (*.txt) DO (
    CD %%~dpg
    IF EXIST *.txt* (
        FOR /F "skip=1 tokens=1* delims=|" %%a IN (%%~nxg) DO (
            SET /a countMetadata+=1
            ECHO %%~dpa%%a^| %%b >> %destinationPath%^%%~nxg
        )
        IF %status% == success (
            ECHO %%g has been successfully added
            ECHO %%g >> %destinationPath%^logs.txt
        )
    )
)
Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

0

After reformatting to use indentation to show code blocks (parenthesised code that is parsed, substituting %variables%, then executed), I then applied various recommendations:

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.

Use set /a to assign numeric values. No quotes required.

THIS CODE WILL NOT FULLY PERFORM THE REQUIRED TASK

Without comments to explain why some code is used, it's difficult to provide guidance.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE (c) ASDG 

SET "locationPath=C:\Textfiles"
SET "destinationPath=E:\Textfiles"


SET "status=success"
SET /a countMetadata=0
SET /a countPDF=0
SET /a countJPEG=0


ECHO Executing the program...
FOR /R "%locationPath%" %%g IN (*.txt) DO (
 CD %%~dpg

 IF EXIST *.txt* (
  FOR /F "usebackq skip=1 tokens=1* delims=|" %%a IN ("%%~nxg") DO (
   SET /a countMetadata+=1

   ECHO %%~dpa%%a^| %%b >> %destinationPath%\%%~nxg
  )
  IF !status! == success (
   ECHO %%g has been successfully added
   ECHO %%g >> %destinationPath%\logs.txt
  )
 )
)
GOTO :EOF

Modifications explanation

I prefer the setlocal to be the second line. YMMV.

Set commands modified as explained above

locationpath in the for /R quoted - it may contain separators like spaces.

%%~nxg quoted in for...%%a as it may contain spaces

usebackq added to for...%%a as %%~nxg is now quoted

I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables) 
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs 
(See `for/f` from the prompt for documentation)

Not sure what is going on with the following echo, but destinationPath no longer has the terminal \. Same comment applies to the following ECHO %%g.

!staus! replaces %status% (logically) as logically, status may change within the block - the delayed expansion trap

BUT status is not being varied within the block, so its value will always be its initial value, success.

Using Boolean

So - a few things for OP to clean up...

Magoo
  • 77,302
  • 8
  • 62
  • 84