China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 11:13
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Challenge - Batch modify file extensions and reverse file names simultaneously View 2,819 Replies 12
Original Poster Posted 2011-01-22 18:32 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
Challenge - Batch Modify File Extensions and Reverse File Names Simultaneously

The purposes of the batch processing are:
1. Batch modify the file extensions of specified types of files in the target folder (including all its subdirectories) to other specified extensions.
2. At the same time, batch reverse the file names of specified types of files in the target folder (including all its subdirectories).

Supplementary Notes -
1. The batch processing must support: the user can enter the target folder (or drag and drop to get the path) directly in the running window of the batch processing, as well as the original extension and the new extension.
2. The path of the target folder may contain spaces.
For example, the directory "C:\Documents and Settings\Administrator".
3. The path of the target folder may contain Chinese characters.
For example, the directory "C:\Documents and Settings\Administrator\Desktop".
4. The path of the target folder may contain special characters.
For example, characters "^" and "&".
5. Specific examples of renaming are:
For example, "1234.original_extension" is renamed to "4321.new_extension".
6. In the case of double extensions:
For example, "1234.jpg.original_extension" is renamed to "gpj.4321.new_extension". In short, only recognize the last extension as the real extension. All the previous ones are treated as the file name as a whole and reversed!
7. The batch processing must support renaming system or hidden attribute files. However, after renaming, the original attributes must be restored.

[ Last edited by lujice on 2011-1-22 at 20:16 ]
Floor 2 Posted 2011-01-22 18:35 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
The following code supports subdirectories, supports paths with special characters, and supports file names with special characters. Does not support reversing file names, does not support paths with spaces, and does not support file names with spaces.


@echo off
:0
cls
echo.
echo. ╭────────╮
echo. ╭─────┤ I N S T R U C T I O N S ├────╮
echo. │ ╰────────╯ │
echo. │ │
echo. │ 1、The purpose of this tool is - batch modify files with specified suffixes in the specified directory to other specified suffixes. │
echo. │ │
echo. │ 2、Please press Enter after each input! │
echo. │ │
echo. │ 3、Note - only enter the suffix, do not enter the "." before the suffix │
echo. │ │
echo. │ 4、If there are spaces and special characters in the folder path, please type it in manually, do not drag and drop! │
echo. │ │
echo. ╰───────────────────╯
echo.&echo Please enter the path of the folder, or drag and drop the folder into this window
set LJ=
set /p LJ=
if /i "%LJ%"=="" goto 0
echo.&echo Please enter the original suffix:
set q=
set /p q=
if /i "%q%"=="" goto 0
echo.&echo Please enter the new suffix:
set h=
set /p h=
if /i "%h%"=="" goto 0
for /r %LJ% %%i in (*.%q%) do ren %%i *.%h%
echo.&echo. Suffixes have been batch modified successfully!
echo.&echo. Please open the folder and take a look!
start %LJ%
pause
goto 0


[ Last edited by lujice on 2011-1-22 at 18:45 ]
Floor 3 Posted 2011-01-22 19:05 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
It's so strange? Why does it support spaces and special characters again?

The following example has roughly the same syntax. But it just wants to support spaces and special characters!

I can't figure it out......




@echo off
:hsz
cls
echo.&echo.&echo Please drag and drop the directory where you want to create the recycle bin to this window!
echo.
set /p mulu=
echo.&echo Please type in or paste the name of the recycle bin.
echo.&echo The way to paste is - right-click in the running window of the batch processing.
echo.&echo If the name of the recycle bin contains spaces, special characters and Chinese characters,
echo.&echo Then you need to add: English double quotes - " before and after the name
echo.
set /p minzi=
md %mulu%\%minzi%
attrib +s +h %mulu%\%minzi%
(echo
echo CLSID={645FF040-5081-101B-9F08-00AA002F954E})>%mulu%\%minzi%\desktop.ini
attrib +s +h %mulu%\%minzi%\desktop.ini
echo.&echo The recycle bin has been successfully created!
echo.&echo Open it and have a look!
echo.
pause
start "" %mulu%
goto (label number of the upper menu)



[ Last edited by lujice on 2011-1-22 at 19:30 ]
Floor 4 Posted 2011-01-22 21:44 ·  中国 湖北 黄石 电信
初级用户
Credits 22
Posts 20
Joined 2009-07-06 23:34
17-year member
UID 148862
Gender Male
Status Offline
Batch-rename files with specified extension by reversing their filenames


@echo off
if "%1" equ "" echo.Required parameter, e.g.: %~0 *.txt&exit/b
SET "LOG=\ReDo.BAT"
for %%i in (%*) do (
cd/d "%%~di"
echo.Processing: "%%~nxi"
call:reverse "%%~nxi"
)
goto:end

:reverse
echo.CD/D "%~dp1">>"%LOG%"
set "fname=%~1"
set "temp_name=%~1"
set new_name=
:LOOP
set temp_name=%temp_name:~0,-1%
if "%temp_name%" equ "" goto:out
set new_name=%new_name%%temp_name:~-1%
goto:LOOP

:out
echo.REN "%new_name%" "%fname%">>"%LOG%"
ren "%fname%" "%new_name%"
goto:eof

:end
echo.Undo, please run: "%LOG%" to cancel
pause
Floor 5 Posted 2011-01-23 11:03 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
The following is the code from Boss "tmplinshi" of the "Batch Processing Home Forum" —



@echo off

:Input_Folder
echo Please enter the folder path (or drag and drop the folder to this window):
set Folder=
set /p Folder=

if not defined Folder goto Input_Folder
if not exist "%Folder:"=%" (
echo * Error: The folder does not exist
goto Input_Folder
)

:Input_OldExt
set OldExt=
set /p OldExt=Please enter the original suffix name:

if not defined OldExt goto Input_OldExt

:Input_NewExt
set NewExt=
set /p NewExt=Please enter the new suffix name:

if not defined NewExt goto Input_NewExt

rem Remove quotes from the path, and the "." and characters before it from the suffix
set "Folder=%Folder:"=%"
set "OldExt=%OldExt:*.=%"
set "NewExt=%NewExt:*.=%"

for /f "delims=" %%i in ('dir /a:-d /b /s "%Folder%\*.%OldExt%"') do (
set fPath=%%~dpi
set fName=%%~ni
set fAttr=%%~ai
SetLocal EnableDelayedExpansion

rem Determine if there are "system" and "hidden" attributes
set AttrList=
if "!fAttr:s=!" neq "!fAttr!" set AttrList= s
if "!fAttr:h=!" neq "!fAttr!" set AttrList=!AttrList! h
if defined AttrList (
attrib !AttrList: = -! "!fPath!!fName!.!OldExt!"
)

rem Calculate the number of characters in the file name
set /a n = 8189, b = 0
for /l %%a in (1 1 13) do (
set /a "a = (n - b) / 2 + b"
for %%b in (!a!) do (
if "!fName:~%%b,1!"=="" (set n=!a!) else set b=!a!
)
)

rem Reverse the file name
set /a n -= 1
for /l %%n in (0 1 !n!) do (
set NewName=!fName:~%%n,1!!NewName!
)

rem Rename
ren "!fPath!!fName!.!OldExt!" "!NewName!.!NewExt!"

rem If the file attributes were modified before, change them back
if defined AttrList (
attrib !AttrList: = +! "!fPath!!NewName!.!NewExt!"
)

EndLocal
)




Tested! No bugs found so far!

This tool is used to prohibit (restore) the execution of certain functions of the computer. For example, prohibit (restore) "My Computer - Right - Click - Manage", for example, prohibit (restore) "Folder Options", for example, prohibit (restore) all items in the Control Panel, prohibit (restore) Group Policy, prohibit (restore) Registry......

Hehe! Can others easily crack this prohibition method???

Thank you very much, Boss tmplinshi!

Go back immediately, pick up the forum's tutorial, and slowly savor Boss tmplinshi's code......

Thank you again, Boss tmplinshi!

[ Last edited by lujice on 2011-1-23 at 11:05 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zaixinxiangnian +1 2011-01-24 00:45
Floor 6 Posted 2011-01-23 11:14 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
Although I am a complete novice, only knowing the simplest commands. But, I dare to try...

In a couple of days, I will post a USB flash drive virus immunity batch processing. There is a bit of original DIY, which is to add a function to hide private files, and strengthen the ability to prevent deletion and renaming.

Experts won't look at it, all are written with simple commands.

Go back and scratch your head
Floor 7 Posted 2011-01-23 20:36 ·  中国 湖北 恩施土家族苗族自治州 电信
初级用户
beginner
Credits 26
Posts 27
Joined 2011-01-13 23:44
15-year member
UID 180774
Gender Male
Status Offline
Regarding the code in the original poster's and that LZ's post, I don't quite understand it. Could you please explain?

In the part where setting the file path is done:
set lj=
set /p lj=
This can assign the path dragged in or input by the person using this batch script to LJ. But later when entering the file extension, it still uses
set q=
or something similar. Then, does it mean that the extension can also be dragged in?
路漫漫其修远兮~
Floor 8 Posted 2011-01-24 12:09 ·  中国 江苏 苏州 太仓市 电信
新手上路
Credits 10
Posts 11
Joined 2010-12-24 12:16
15-year member
UID 179776
Gender Male
Status Offline
Originally posted by modestleaner at 2011-1-23 20:36:
I don't quite understand the little code snippet posted by the LZ and the original poster. I hope for an explanation.
In the part where the file path is set:
set lj=
set /p lj=
It can assign the path dragged in or entered by the person using this batch file to...


set lj=
rem Set variable name
set /p lj=
rem Assign the obtained value to lj

Personal understanding, please correct any errors.
Floor 9 Posted 2011-01-24 15:43 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
Originally posted by modestleaner at 2011-1-23 20:36:
I don't quite understand the code snippet posted by the original poster and that LZ. Hope for an explanation.
In the part where the file path is set:
set lj=
set /p lj= can assign the path dragged in or entered by the person using this batch script to...



First of all, I declare that I am also a very, very rookie, and I don't even understand the for statement very well. Let's learn together!

@echo off (do not display executed commands)
:hsz (this section's label number)
echo.&echo.&echo Please drag and drop the directory for which you want to create the recycle bin into this window!
echo.
set /p mulu= (assign a value to variable %mulu%, supports hand input or drag and drop)
echo.&echo Please hand-enter or paste the name of the recycle bin.
echo.&echo The way to paste is - right-click in the running window of the batch script.
echo.&echo If the name of the recycle bin contains spaces, special characters, and Chinese characters,
echo.&echo then you need to add: English double quotes before and after the name - "
echo.
set /p minzi== (hand-enter to assign a value to variable %minzi%)
md %mulu%\%minzi% (create a folder with the specified name in the specified directory)
attrib +s +h %mulu%\%minzi% (change its attributes to system and hidden)
(echo
echo CLSID={645FF040-5081-101B-9F08-00AA002F954E})>%mulu%\%minzi%\desktop.ini (write the characters in the parentheses to the "desktop.ini" file under the target folder)
attrib +s +h %mulu%\%minzi%\desktop.ini (change the attribute of the "desktop.ini" file to system and hidden)
echo.&echo The recycle bin has been successfully created!
echo.&echo Open it and take a look!
echo.
pause (pause)
start "" %mulu% (open the directory where the recycle bin is created)
goto (label number of the upper menu)

[ Last edited by lujice on 2011-1-24 at 15:47 ]
Floor 10 Posted 2011-01-24 18:06 ·  中国 湖北 恩施土家族苗族自治州 电信
初级用户
beginner
Credits 26
Posts 27
Joined 2011-01-13 23:44
15-year member
UID 180774
Gender Male
Status Offline
Originally posted by glodboy at 2011-1-24 12:09:


set lj=
rem Set variable name
set /p lj=
rem Assign the obtained value to lj

Personal understanding, please correct any mistakes.

First of all, thank you to the above-mentioned friends for their answers, but I'm afraid the brother here has an incorrect understanding
The function of /p is to allow the variable value to be set as a line of input from the user.
I understand the part about creating a recycle bin in the specified folder, but I still don't understand that sentence.
路漫漫其修远兮~
Floor 11 Posted 2011-01-24 19:47 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
Originally posted by modestleaner at 2011-1-23 20:36:
There is a bit of confusion regarding the original poster and the code snippet the LZ posted. Hope to get an explanation.
In the part where the file path is set:
set lj=
set /p lj=
This can assign the path dragged in by the person using this batch script or entered by them to...


The file extension cannot be dragged in. After dragging a file in, the full path including the file name is displayed. Unless you use the backspace key to delete the extra characters and only leave the file extension. But what's the difference between this and typing in the file extension manually?

[ Last edited by lujice on 2011-1-24 at 19:49 ]
Floor 12 Posted 2011-01-24 19:48 ·  中国 四川 成都 电信
新手上路
Credits 12
Posts 13
Joined 2010-03-03 10:47
16-year member
UID 161489
Gender Male
Status Offline
Originally posted by modestleaner at 2011-1-23 20:36:
I don't quite understand the code snippet posted by the original poster and that LZ. Hope for an explanation.
In the part where the file path is set:
set lj=
set /p lj=
This can assign the path dragged in or entered by the person using this batch file to...


The file extension cannot be dragged in. After dragging a file in, the full path including the file name is displayed. Unless you use the backspace key to delete the excess characters and only leave the file extension. But what's the difference between doing this and typing in the file extension manually?
Floor 13 Posted 2011-01-25 00:48 ·  中国 湖北 恩施土家族苗族自治州 电信
初级用户
beginner
Credits 26
Posts 27
Joined 2011-01-13 23:44
15-year member
UID 180774
Gender Male
Status Offline
Originally posted by lujice at 2011-1-24 19:48:

The file extension cannot be dragged in. After dragging the file in, the full path including the file name is displayed. Unless you use the backspace key to delete the extra characters and only leave the file extension. But what's the difference between doing this and typing the file extension manually?

Hmm, I know that the file extension can't be dragged in. What I mean is that then is that sentence redundant?
Since the extension can't be dragged in and can only be entered manually.
路漫漫其修远兮~
Forum Jump: