@@ -162,21 +162,32 @@ private void MakeUnique()
162162 EditModulesLua ( ) ;
163163 }
164164
165- private static void BulkRename ( string fPath , string match , string fNewName )
165+ private static void BulkRename ( string folderPath , string match , string replacement )
166166 {
167- foreach ( FileInfo f in new DirectoryInfo ( fPath ) . GetFiles ( ) )
168- {
169- string fromName = Path . GetFileNameWithoutExtension ( f . Name ) ;
167+ if ( string . IsNullOrEmpty ( match ) )
168+ throw new ArgumentException ( "match must not be empty" , nameof ( match ) ) ;
169+
170+ DirectoryInfo dir = new ( folderPath ) ;
170171
171- if ( ! fromName . Contains ( match ) )
172+ foreach ( var file in dir . EnumerateFiles ( ) )
173+ {
174+ var baseName = Path . GetFileNameWithoutExtension ( file . Name ) ;
175+ if ( baseName is null || ! baseName . Contains ( match , StringComparison . Ordinal ) )
172176 continue ;
173177
174- string ext = Path . GetExtension ( f . Name ) ;
178+ var ext = file . Extension ;
179+
180+ var newBaseName = baseName . Replace ( match , replacement , StringComparison . Ordinal ) ;
181+
182+ var targetPath = Path . Combine ( file . DirectoryName ! , newBaseName + ext ) ;
183+
184+ if ( string . Equals ( file . FullName , targetPath , StringComparison . OrdinalIgnoreCase ) )
185+ continue ;
175186
176- fromName = Path . Join ( fPath , f . Name ) ;
177- string toName = Path . Join ( fPath , fNewName ) + ext ;
187+ if ( File . Exists ( targetPath ) )
188+ throw new IOException ( $ "Target file already exists: { targetPath } " ) ;
178189
179- File . Move ( fromName , toName ) ;
190+ file . MoveTo ( targetPath ) ;
180191 }
181192 }
182193
0 commit comments