我正在对大量带有大量 jpg 的目录进行重大重组,其中一些与其他目录中的文件具有相同的名称。我想移动/文件复制到备用目录,并具有自动的bash如果该名称在目录中的另一个文件重命名匹配他们(重命名IMG_238.jpg到IMG_238_COPY1.jpg,IMG_238_COPY2.jpg等),而不是覆盖现有文件。
我已经设置了一个脚本,它接受 jpeg 并将它们移动到基于 exif 数据的新目录。移动一个 jpg 的脚本的最后一行是:mv -n "$JPEGFILE" "$DIRNAME"
我正在使用 -n 选项,因为我不想覆盖文件,但现在我必须手动对没有移动/复制的文件进行排序。我的 GUI 会自动执行此操作...在 bash 中是否有相对简单的方法来执行此操作?
(以防万一,我在 Mac OSX Lion 中使用 bash 3.2)。
好的,我知道 REN 命令的典型格式,REN source dest并且有几个使用通配符重命名的示例。
例子:
REN *.txt *.doc
这会将您的所有 .txt 文件重命名为 .doc。好吧,这一切都很好,但我有一堆扩展名为 .aaaa.bbbb 的文件,当我使用命令时:
REN *.aaaa.bbbb *.aaaa
我得到了所有扩展名为 .aaaa.aaaa 的文件。现在我无法想出一个 REN 命令来获得 .aaaa 扩展名。
我想将文件夹中的所有文件重命名为随机名称,但它想将所有文件重命名为相同的名称:
ren "c:\Test\*.txt" %Random%.txt
pause
Run Code Online (Sandbox Code Playgroud)
输出:
C:\Users\Oliver\Desktop>ren "c:\Test\*.txt" 9466.txt
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
A duplicate file name exists, or the file
cannot be found.
C:\Users\Oliver\Desktop>pause
Press any key to …Run Code Online (Sandbox Code Playgroud) 考虑一下我们将以下命令应用于hello.txtgit 下跟踪的文件(在干净的工作副本中):
echo "hi" >> hello.txt
mv hello.txt bye.txt
git rm hello.txt
git add bye.txt
git status
Run Code Online (Sandbox Code Playgroud)
结果:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: hello.txt -> bye.txt
Run Code Online (Sandbox Code Playgroud)
因此,git 知道它是同一个文件,即使它被重命名了。我有一些模糊的记忆,git 检查 inode 以确定新文件与旧的已删除文件相同。 不过,这个和这个SO 答案表明 git 只检查文件的内容,并且不会以任何方式检查它是否是相同的 inode。(我的结论(*):如果我对文件进行更大的修改,git 将不会检测到重命名,即使 inode 仍然相同。)
因此,在我看来,很明显,我错了,git 不检查 inode(或任何其他文件系统信息),只检查内容。但后来,我发现了另一个答案,它声称
除了时间戳之外,它[即git]还记录lstat的大小、inode和其他信息,以减少误报的机会。当您执行 git-status 时,它只需对工作树中的每个文件调用 lstat 并比较元数据,以便快速确定哪些文件未更改。
我对此实际上有两个问题:
Git 确实依赖(也)依赖 inode 来检测文件是否已更改,但它不使用 inode 来检测文件重命名。
我想删除一个文件,例如Foo1.aspx,然后将另一个文件Foo2.aspx重命名为Foo1.aspx.但是,即使在删除之后,我也无法重命名Foo2.aspx,大概是因为TFS保留原始文件以用于恢复目的.
有没有办法做到这一点,除了选择Foo2的内容.aspx并将其粘贴到Foo1.aspx中?
我的Prevalidation文件夹中有许多扩展名为.psa的文件,我想:
对于源文件夹中的所有.psa文件,将重复此操作.
因此,问题是我如何在循环中执行与当前一样多的.psa文件的命令集.
这是我的Prevalidation文件夹中只有一个文件的代码测试 -
Copy-Item C:\Downloads\PreValidation\*.psa C:\Downloads\Validation\WIP
Rename-Item 'C:\Downloads\Validation\WIP\abc 1234.psa' 'psaload1.csv'
Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
<run the psaload.csv to load to my db>
Run Code Online (Sandbox Code Playgroud)
这是我打算做的 -
考虑我的C:\ Downloads\Prevalidation文件夹中的多个.psa文件.
For each C:\Downloads\PreValidation\*.psa
BEGIN LOOP
Copy-Item C:\Downloads\PreValidation\aaaa.psa C:\Downloads\Validation\WIP\aaaa.psa
Rename-Item 'C:\Downloads\Validation\WIP\aaaa.psa' 'psaload1.csv'
Get-Content C:\Downloads\Validation\WIP\psaload1.csv | ForEach-Object { $_.replace("\,"," ") } | Set-Content C:\Downloads\Validation\WIP\psaload.csv
Remove-Item C:\Downloads\Validation\WIP\psaload1.csv
END LOOP
Run Code Online (Sandbox Code Playgroud)
我正在寻找语法来为我/ prevalidation文件夹中的每个文件逐个运行这些命令集.
我正在尝试移动和重命名我从TCP服务器收到的文件.
我的移动和重命名代码:
*//My sourcePath*
static string myServerfile = @"C:\Users\me\Documents\file_client\bin\Debug\test1.txt";
*//My destinationPath*
static string myFile = @"C:\test\inbox\JobStart.txt";
Run Code Online (Sandbox Code Playgroud)
收到文件后我这样做:
fs.Close ();
serverStream.Close ();
File.Move(myServerfile, myFile);
Console.WriteLine("Moved");
}
catch (Exception ex)
{
Console.WriteLine ("Cannot be DONE!");
}
Run Code Online (Sandbox Code Playgroud)
但是当它达到时,总是抛出异常"无法完成" File.Move(myServerfile, myfile1);
我试过这个:
Console.WriteLine(ex.ToString());
结果: System.IO.IOException:无法创建已存在的文件.

我究竟做错了什么?
我有各种名字的文件
"type_featAtype_featB"
和
"type_featAtype_featC"
我需要这一切
"type_featA"
被替换为
"开始"
所以将调用文件夹的新名称
"STARTtype_featB"
和
STARTtype_featC"
.
但是,并非所有文件都具有相同的后缀,实际上它们都有不同的后缀,如示例中所示.
因此,我正在寻找有关如何使用命令来替换文件名的一部分的见解,如上面在一个文件夹中所示,其中每个文件具有唯一的后缀.谢谢
预先感谢您的任何帮助.
我正致力于将视频文件保存在iOS应用程序的文档文件夹中.
我想以编程方式重命名某些文件,但我不知道该怎么做.
有没有任何解决方案???
感谢致敬.
假设我有一个包含n个csv文件的文件夹,我想重命名.新的文件名将是一个类似的ABxxxx,xxxx从1到1000的渐进数字.
在这样做时,我如何保留原始文件扩展名,这是csv什么?
到目前为止我所做的已经改变了文件名,但已经删除了扩展名:
directory=r'C:\Me\MyDir'
subdir=[x[0] for x in os.walk(directory)]
subdir.pop(0)
for i in subdir:
temp_dir=r''+i
os.chdir(temp_dir)
a='A'
b='B'
for file in glob.glob("*.csv"):
for i in range(1,1001):
newname=a+b+i
os.rename(file,newname)
Run Code Online (Sandbox Code Playgroud)