0

I have I alrdy looked at this question: How to replace substrings in windows batch file And also this one ; How can you find and replace text in a file using the Windows command-line environment? My problem is around this : How to pass path with spaces to script for when I search for a specific string within my CSV file, which contains folder path location I get the error below ; listed under ERROR . My end goal is to just get from sample data 1415, 1417 and 1419. BUT I'm willing to do 2 bat file; powershell file calls if it gets me there. My code in BAT FILE is as following

powershell -ExecutionPolicy ByPass -File C:\Users\xyz\Desktop\bat_file_compare_TEST\step_3_Replace_extra_data_with_just_Store_no.ps1
pause

and my code in powershell called by BAT file above is . Error is after this and depending on if i use single quotes vs double my error is right around replace. (2) I have also tried "gc" instead of get-content. (3) no quotes around the somereport_generated_store_filenames.csv .

powershell -Command "(Get-Content 'C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_store_filenames.csv') -replace 'C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_files\somereport-', 'test' | Out-File 'C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_store_filenames_just_Store_no.csv' -encoding ASCII"

Error:-

The regular expression pattern C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_files\somereport is not valid.
At line:1 char:1
+ (Get-Content 'C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_genera ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (C:\Users\xyz\...rated_files\somereport:String) [], RuntimeException
    + FullyQualifiedErrorId : InvalidRegularExpression

Finally Sample Data if anyone wishes to re-create this is:

C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_files\somereport-2022M01-1415.pdf
C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_files\somereport-2022M01-1417.pdf
C:\Users\xyz\Desktop\bat_file_compare_TEST\somereport_generated_files\somereport-2022M01-1419.pdf

1 Answer 1

0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74072170.txt"

FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
 SET "name=%%~nb"
 SET "name=!name:-= !"
 FOR %%e IN (!name!) DO SET "name=%%e"
 ECHO !name!
)

GOTO :EOF

Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.

The format of the data is absolutefilename, so simply read each line to %%b, copy the name part to name, change each - to a space and set a variable (name again) to each value found in turn, so that variable eventually contains the very last group of characters; then echo it.

Sign up to request clarification or add additional context in comments.

1 Comment

ty sir let set this up and I will leave positive feed back or question if I have any. Thank you in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.