更改名称为什么用 Run 可以,用 FileMove 就不行?

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

shendaowu
Posts: 4
Joined: 01 Mar 2023, 06:17

更改名称为什么用 Run 可以,用 FileMove 就不行?

24 Mar 2024, 00:55

功能是先将脚本所在路径下面的 files 文件夹里的文件读入到一个文本文件。然后对这个文件文件进行手动排序,然后根据排序的结果进行重命名。
比如文件夹里有 001.a.mp3 002.b.mp3 003.c.mp3。然后修改文本文件中的内容为 002.b.mp3 003.c.mp3 001.a.mp3,然后继续执行会将文件重命名为 001.b.mp3 002.c.mp3 003.a.mp3。

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

FileList := ""  ; Initialize to be blank.
Loop, .\files\*.*
    FileList .= A_LoopFileName "`n"
;Sort, FileList, R  ; The R option sorts in reverse order. See Sort for other options.

FileDelete, .\test.txt
FileAppend, %FileList%, .\test.txt

MsgBox,,, 继续

FileRead, newFileList, test.txt
newFileArray := StrSplit(newFileList, "`n")

FileDelete, .\test2.txt
For key, oldFileName in newFileArray{
    newFileName := Format("{:03}", A_Index) . "." . SubStr(oldFileName, 5)
    oldFileName := ".\files\" . oldFileName
    ;newFileName := ".\files\" . newFileName
    FileAppend, %oldFileName%`n, .\test2.txt
    FileAppend, %newFileName%`n`n, .\test2.txt
    ;FileMove, %oldFileName%, %newFileName%
    Run, %ComSpec% /c rename "%oldFileName%" "%newFileName%" 
}

MsgBox,,, 继续
shendaowu
Posts: 4
Joined: 01 Mar 2023, 06:17

Re: 更改名称为什么用 Run 可以,用 FileMove 就不行?

24 Mar 2024, 04:47

自己大概解决了,大概是换行惹的祸。

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

FileList := ""  ; Initialize to be blank.
Loop, .\files\*.*
    FileList := FileList . A_LoopFileName . "`n" ; 这里是不是有问题?
;Sort, FileList, R  ; The R option sorts in reverse order. See Sort for other options.

FileDelete, .\manual_sort.txt
FileAppend, %FileList%, .\manual_sort.txt

Run, .\manual_sort.txt
MsgBox,,, 继续

FileRead, newFileList, .\manual_sort.txt
newFileArray := StrSplit(newFileList, "`n")


FileDelete, .\test2.txt
For key, oldFileName in newFileArray{
    FileAppend, %oldFileName%`n, .\test2.txt
    ;oldFileName := Trim(oldFileName, OmitChars = "`n")
    oldFileName := StrReplace(oldFileName, "`r", "")
    oldFileName := StrReplace(oldFileName, "`n", "")
    if(oldFileName = ""){
        Continue
    }
    newFileName := Format("{:03}", A_Index) . "." . SubStr(oldFileName, 5)
    oldFileName := ".\files\" . oldFileName
    newFileName := ".\files\" . newFileName
    ;FileAppend, %oldFileName%`n, .\test2.txt
    ;FileAppend, %newFileName%`n`n, .\test2.txt
    FileMove, %oldFileName%, %newFileName%
    ;Run, %ComSpec% /c rename "%oldFileName%" "%newFileName%"
    ;FileAppend, rename "%oldFileName%" "%newFileName%"`n, .\test2.txt
}

MsgBox,,, 继续
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: 更改名称为什么用 Run 可以,用 FileMove 就不行?

03 Apr 2024, 09:12

其他例子 :

Code: Select all

#Requires AutoHotkey v1.1
#NoEnv
#Warn
SetWorkingDir,%A_ScriptDir%
;-
filelist:=""
FD1:=a_scriptdir . "\Files"
Loop,%fd1%\*.*
    FileList .=A_LoopFileName . "`r`n"  ;- 这里是不是有问题?
msgbox, 262208,FILELIST,%filelist%
exitapp
FileRename in FD1 -----------

Code: Select all

;- FileRename in FD1 -----------
#Requires AutoHotkey v1.1
#warn
#singleinstance,force
setworkingdir,%a_scriptdir%
FD1:=a_scriptdir . "\Files"
i:=0
Loop,Files,%FD1%\*.* 
{
i++
i:=SubStr(000 I, -3)
aa:="0001_"
if a_loopfilename contains %aa%
  {
  msgbox, 262208, ,Files in =`n%FD1%`n are already renamed
  break
  }
newname:=I . "_" . a_loopfilename
new:=fd1 . "\" . newname
filemove,%A_LoopFileFullPath%,%new%,1
}   
try,run,%fd1%
exitapp
;---------------
esc::exitapp
;===============
Filemove ( or copy ) from FD1 to FD2

Code: Select all

;- Filemove ( or copy )  from FD1 to FD2 ----
#Requires AutoHotkey v1.1
#warn
#singleinstance,force
setworkingdir,%a_scriptdir%
FD1:=a_scriptdir . "\Files"
FD2:=a_scriptdir . "\NEW_Files"
If !FileExist(fd2)
	FileCreateDir,%fd2%
Loop,Files,%FD1%\*.* , R 
{
new:=fd2 . "\" . a_loopfilename
filemove,%A_LoopFileFullPath%,%new%,1
}   
try,run,%fd2%
exitapp
;---------------
esc::exitapp
;===============

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 130 guests