有没有办法在保持原始创建/修改/读取时间的同时重命名文件?这是在 Solaris 中。
提前致谢。
我有一个 Delphi 2006 应用程序,它写入一个文件,然后定期将其重命名为有序名称,并创建一个新文件,即
open file.dat
write a record to file.dat
close file.dat
...
open file.dat
write a record to file.dat
close file.dat
rename file.dat to file20110818123456.dat
create file.dat
open file.dat
write a record to file.dat
close file.dat
...
Run Code Online (Sandbox Code Playgroud)
问题是有时重命名会失败并显示Error 32 - The process cannot access the file because it is being used by another process. 我正在检查重命名的目标是否不存在。就好像写入记录后文件的关闭不会立即发生。我在重命名过程中添加了一个循环来休眠一会儿,然后重试最多 10 次,但这没有帮助。
function RenameFileAtAllCosts (const OldFileID : TFilename ;
const NewFileID : TFilename ;
out ErrorCode : Integer) : boolean ; …Run Code Online (Sandbox Code Playgroud) 我在同一个目录中有一堆文件,名称如下:
IMG_20160824_132614.jpg
IMG_20160824_132658.jpg
IMG_20160824_132738.jpg
中间部分是日期,最后部分是照片拍摄时间。因此,如果我按名称对这些文件进行排序,结果将与按修改的日期/时间排序相同
我想使用 bash 将这些文件批量重命名为以下形式:
1-x-3.jpg
其中x代表文件在顺序排序中的位置(按名称/修改时间排序)
因此,上面的 3 个示例将重命名为:
1-1-3.jpg
1-2-3.jpg
1-3-3.jpg
是否有可以实现此目的的 bash 命令?还是需要脚本?
我试图通过删除其基本名称中的最后四个字符来重命名子目录中存储的许多文件。我通常使用以下方法在一个目录glob.glob()中查找和重命名文件:
import glob, os
for file in glob.glob("C:/Users/username/Desktop/Original data/" + "*.*"):
pieces = list(os.path.splitext(file))
pieces[0] = pieces[0][:-4]
newFile = "".join(pieces)
os.rename(file,newFile)
Run Code Online (Sandbox Code Playgroud)
但现在我想在所有子目录中重复上述内容。我尝试使用os.walk():
import os
for subdir, dirs, files in os.walk("C:/Users/username/Desktop/Original data/"):
for file in files:
pieces = list(os.path.splitext(file))
pieces[0] = pieces[0][:-4]
newFile = "".join(pieces)
# print "Original filename: " + file, " || New filename: " + newFile
os.rename(file,newFile)
Run Code Online (Sandbox Code Playgroud)
该print语句正确打印我正在查找的原始文件名和新文件名,但os.rename(file,newFile)返回以下错误:
Traceback (most recent call last):
File "<input>", line 7, in <module> …Run Code Online (Sandbox Code Playgroud) 我编写的脚本(应该)执行以下操作时遇到了持续的问题。我有一个文件夹,其中包含许多 csv 文件,我想将带有公司名称的最新文件复制到另一个文件夹中,并重命名它。
当前格式为:
21Feb17070051_CompanyName_Sent21022017
Run Code Online (Sandbox Code Playgroud)
我希望它采用以下格式:
CompanyName21022017
Run Code Online (Sandbox Code Playgroud)
所以我有以下 powershell 脚本来执行此操作:
## Declare variables ##
$DateStamp = get-date -uformat "%Y%m%d"
$csv_dest = "C:\Dest"
$csv_path = "C:\Location"
## Copy latest Company CSV file ##
get-childitem -path $csv_path -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime |
select-object -last 1 |
copy-item -Destination $csv_dest
## Rename the file that has been moved ##
get-childitem -path $csv_dest -Filter "*Company*.csv" |
where-object { -not $_.PSIsContainer } |
sort-object -Property $_.CreationTime | …Run Code Online (Sandbox Code Playgroud) 我有一个需要在任何平台(Windows、Mac、Linux 等)上运行的 Perl 脚本。它的部分功能是重命名文件,但我不希望它覆盖现有文件。假设该脚本名为“my_rename”,它采用与“rename”函数相同的参数,并且用户执行以下命令:
my_rename test.txt test.TXT
Run Code Online (Sandbox Code Playgroud)
-e "test.txt"如果和-e "test.TXT"都返回 true,就会出现问题。以下是我想在以下条件下处理这种情况的方法:
情况 1:在区分大小写的文件系统上:
情况 2:在不区分大小写的文件系统上,当现有文件名的实际大小写为“test.TXT”时:
情况 3:在不区分大小写的文件系统上,当现有文件名的实际大小写不是“test.TXT”时:
由于该脚本必须是可移植的,因此它不能依赖于系统相关的函数或实用程序。
在PHP中是否有任何漂亮的解决方案,如果文件名已存在,我可以使用自动递增编号扩展文件名?我不想在一些不可读的东西中重命名上传的文件.所以我觉得这样会很好:(允许所有图像文件.)
Cover.png
Cover (1).png
Cover (2).png
…
Run Code Online (Sandbox Code Playgroud) 我正在运行此PowerShell命令:
Get-ChildItem .\tx\*.htm | Rename-Item -NewName {$_.Name -replace '\.htm','.tmp'}
Run Code Online (Sandbox Code Playgroud)
当文件名包含方括号 - [和/或]- 时,会收到以下错误,这是可以理解的,因为这些在PowerShell语法中有意义.
Rename-Item : Cannot rename because item at
'Microsoft.PowerShell.Core\FileSystem::C:\users\xxxxx\desktop\tx\
Foofoofoofoo_foo_foo_[BAR]_Foofoofoofoo_foofoofoo.htm' does not exist.
At C:\users\xxxxx\desktop\foo002.ps1:59 char:39
+ Get-ChildItem .\tx\*.htm | Rename-Item <<<< -NewName { $_.Name -replace '\.htm','.tmp' }
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
路径中的所有其他文件具有相似的名称(Phrase_With_Underscores.htm),并且无需重大事件即可重命名.任何人都有这方面的经验,知道如何反击它,以便我可以操纵这些文件?
我的文件系统中有很多文件,目录和子目录.
例如:
/path/to/file/test-poster.jpg
/anotherpath/my-poster.jpg
/tuxisthebest/ohyes/path/exm/bold-poster.jpg
我想从切换所有文件名*-poster.jpg,以folder.jpg
我与尝试sed并awk没有成功.
一点帮助?
为什么文件重命名失败?
我的操作系统是Windows 7 C:/test/dfhsdfhs,文件系统中存在文件夹.
我的代码:
String path = "C:/test/dfhsdfhs/test2.txt";
boolean hasDeleteFolder = true;
File delFile = new File(path);
if (delFile.exists()) {
if (hasDeleteFolder == true) {
Date dateTimeNow = new Date();
String _dateTimeNowStr = dateTimeNow.toString();
_dateTimeNowStr = _dateTimeNowStr.replace(" ", "_");
File timeStampFile = new File (delFile.getAbsolutePath() + "_" + _dateTimeNowStr + "." + FilenameUtils.getExtension(delFile.getName()));
if (delFile.renameTo(timeStampFile)) {
System.out.println("renamed");
} else {
System.out.println("Error");
}
}
}
Run Code Online (Sandbox Code Playgroud) file-rename ×10
powershell ×2
rename ×2
awk ×1
bash ×1
batch-rename ×1
copy ×1
delphi ×1
delphi-2006 ×1
file ×1
file-upload ×1
java ×1
linux ×1
os.walk ×1
perl ×1
php ×1
python ×1
sed ×1
shell ×1
solaris ×1
unix ×1