1

(I DIDN'T DO A BATCH WITH THIS STUFF YET)

I have a file called "worldcopy2.txt" which inside has a full directory:

C:\Users\Robert\Desktop\myworld2

And I want to create a batch file to change the inside of the .txt to only have the name of the last folder of the directory that be in the moment. For example:

If the .txt file says C:\Users\Robert\Desktop\funinside, the batch must change the .txt to say inside only funinside.

Must work with all the directories.

Translated with Google Translate.

es2305
  • 25
  • 4

1 Answers1

1

I have tried this:

@echo off
setlocal
REM read a full path from file
type worldcopy2.txt
for /f %%F in (worldcopy2.txt) do set x=%%F
REM extract last path component and write it back to file
for %%P in ("%x%") do echo %%~nP> worldcopy2.txt

type worldcopy2.txt

Discard the type worldcopy2.txt statements after testing. The idea behind this is to treat the path as if it were a fully qualified filename and let the for loop extract the name. Note that this will fail if the foldername contains a dot (".").

user1016274
  • 4,071
  • 1
  • 23
  • 19
  • I need that the batch read the full directory that It is in "worldcopy2.txt" and after read the txt document, it change the file "worldcopy2.txt" to only have the name of the folder. Example: If the .txt file says C:\Users\Robert\Desktop\funinside, the batch must change the .txt to only say inside "funinside". Also, thanks for your help ;) – es2305 May 03 '15 at 20:54
  • Isn't that **exactly** what you described in your question? A text file contains a full path, and the batch command should change it into only the last component. Please clarify - I think Google translate lost some of your intentions in the process. – user1016274 May 03 '15 at 20:57
  • It worked, thanks a lot, but If the directory is like that: C:\Users\Robert\Documents\test it will change the .txt to say "Documents", not "test" :/ – es2305 May 03 '15 at 21:07
  • Yes, that is the only exception for this code - I mentioned it. If you want to avoid this, the code will look like: get the file content into a string, replace all backslashes with ";" and loop over this using "delims=;". You can find code like this on SO using the search function. – user1016274 May 03 '15 at 21:17