1

I'm trying to automate renaming of files based on a CSV such as the one shown below:

Name,FullName

John,JohnDoe

Jane,JaneDoe

Joe,JoeDoe

Let's say I have 3 text files within the same folder of my .bat called John.txt, Jane.txt, Joe.txt and I want to rename John.txt to JohnDoe.txt, etc.

I am getting "The system cannot find the file specified" no matter how much I alter the filepath in my rename. Here is a basic rundown of what I have. What am I doing wrong here or what other way should I approach this? I appreciate all feedback.

@echo off
setlocal enabledelayedexpansion

set csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv

FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (!csvpath!) do (
    set person=%%g
    set name=%%h

    echo My name is !person! and my full name is !name!

    rename !person!.txt !name!.txt
)

pause
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Jen
  • 13
  • 2
  • 1
    Is the script in the same folder as the CSV files? If it isn't, that would cause the error and you'd have to `cd C:\Users\user1\OneDrive\Documents\BatchExamples` at the top of the script before doing any renaming. – SomethingDark May 15 '22 at 17:19
  • 1
    …`cd /D "%UserProfile%\OneDrive\…`… – aschipfl May 15 '22 at 17:43
  • 1
    You have an error in your parenthesized `for` command; ```!csvpath!``` should be ```"!csvpath!"```. In addition you should improve your syntax; ```set csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv``` should read as ```set "csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv"```, ```set person=%%g```, should be ```set "person=%%g"```, ```set name=%%h```, should be ```set "name=%%h"```, and ```rename !person!.txt !name!.txt``` should be ```rename "!person!.txt" "!name!.txt"``` – Compo May 15 '22 at 18:04

1 Answers1

0

This is how I would do it:

@echo off

set "csvpath=C:\Users\user1\OneDrive\Documents\BatchExamples\stuff.csv"

FOR /F "usebackq skip=1 tokens=1,2 delims=," %%g IN (`findstr /v /c:":-:-:" "%csvpath%"`) do (
    echo My name is "%%g" and my full name is "%%h"

    rename "%~dp0\%%g.txt" "%%h.txt"
)

pause

This code is a bit cleaner and more robust, in that file paths and names can have special characters (like &) without breaking the script.

findstr /v /c:"SEARCHSTRING" "FILEPATH" tells findstr to print every line within FILEPATH excluding (/v) lines with SEARCHSTRING. This doesn't really change much from what you had previously, however it is a bit more robust.

In the rename command, I set it to %~dp0 and then the file name, %~dp0 is the path to where your .bat script is.

Styris
  • 133
  • 1
  • 7
  • Thank you for the clear explanation and advice! It's working for me now and I'm playing around with it some more to absorb what you just explained. Much appreciated. – Jen May 15 '22 at 19:20
  • @Jen No problem. If you want to mark my answer as the correct one, feel free to click the checkmark just under the downvote button on my answer, and we'll both get some points :). – Styris May 15 '22 at 20:10