仅保留带有完整目录的.txt文件中文件夹的名称(批处理)

时间:2021-05-02 00:29:06

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

(我没有用这个东西做批次)

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

我有一个名为“worldcopy2.txt”的文件,里面有一个完整的目录:

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:

我想创建一个批处理文件来更改.txt的内部,使其只具有当前目录的最后一个文件夹的名称。例如:

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

如果.txt文件显示为C:\ Users \ Robert \ Desktop \ funinside,则批处理必须将.txt更改为仅在funinside内部。

Must work with all the directories.

必须适用于所有目录。

Translated with Google Translate.

翻译与谷歌翻译。

1 个解决方案

#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 (".").

测试后丢弃worldcopy2.txt类型的语句。这背后的想法是将路径视为完全限定的文件名,并让for循环提取名称。请注意,如果foldername包含一个点(“。”),则会失败。

#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 (".").

测试后丢弃worldcopy2.txt类型的语句。这背后的想法是将路径视为完全限定的文件名,并让for循环提取名称。请注意,如果foldername包含一个点(“。”),则会失败。